<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qsettings.cpp -->
  <title>QSettings 类 | Qt Core 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtcore-index.html">Qt Core</a></td><td ><a href="qtcore-module.html">C++ 类</a></td><td >QSettings</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtcore-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#public-types">公用类型</a></li>
<li class="level1"><a href="#public-functions">公用函数</a></li>
<li class="level1"><a href="#static-public-members">静态公用成员</a></li>
<li class="level1"><a href="#reimplemented-protected-functions">重新实现的保护函数</a></li>
<li class="level1"><a href="#details">详细描述</a></li>
<li class="level2"><a href="#basic-usage">Basic Usage</a></li>
<li class="level2"><a href="#qvariant-and-gui-types">QVariant and GUI Types</a></li>
<li class="level2"><a href="#section-and-key-syntax">Section and Key Syntax</a></li>
<li class="level2"><a href="#fallback-mechanism">Fallback Mechanism</a></li>
<li class="level2"><a href="#restoring-the-state-of-a-gui-application">Restoring the State of a GUI Application</a></li>
<li class="level2"><a href="#accessing-settings-from-multiple-threads-or-processes-simultaneously">Accessing Settings from Multiple Threads or Processes Simultaneously</a></li>
<li class="level2"><a href="#platform-specific-notes">Platform-Specific Notes</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QSettings 类</h1>
<!-- $$$QSettings-brief -->
<p><a href="qsettings.html">QSettings</a>类提供持久的独立于平台的应用程序设置。<a href="#details">更多…</a></p>
<!-- @@@QSettings -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign">头文件：</td><td class="memItemRight bottomAlign">   <span class="preprocessor">#include &lt;QSettings&gt;</span>
</td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += core</td></tr><tr><td class="memItemLeft rightAlign topAlign">继承自：</td><td class="memItemRight bottomAlign"> <a href="qobject.html">QObject</a></td></tr></table></div><ul>
<li><a href="qsettings-members.html">所有成员的列表，包括继承的成员</a></li>
<li><a href="qsettings-obsolete.html">过时的成员</a></li>
</ul>
<p><b>注意:</b> 这个类中的所有函数都是<a href="../qtdoc/threads-reentrancy.html">可重入的</a><span lang="zh-cn">。</span></p>
<p><b>注意:</b> 这些函数也是<a href="../qtdoc/threads-reentrancy.html">线程安全的</a>:</p>
<ul>
<li><a href="qsettings.html#registerFormat">registerFormat</a>(const QString &amp;extension, QSettings::ReadFunc readFunc, QSettings::WriteFunc writeFunc, Qt::CaseSensitivity caseSensitivity)</li>
</ul>
<a name="public-types"></a>
<h2 id="public-types">Public<span lang="zh-cn">类型</span></h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> enum </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#Format-enum">Format</a></b> { NativeFormat, Registry32Format, Registry64Format, IniFormat, InvalidFormat }</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> typedef </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#ReadFunc-typedef">ReadFunc</a></b></td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> enum </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#Scope-enum">Scope</a></b> { UserScope, SystemScope }</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> typedef </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#SettingsMap-typedef">SettingsMap</a></b></td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> enum </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#Status-enum">Status</a></b> { NoError, AccessError, FormatError }</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> typedef </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#WriteFunc-typedef">WriteFunc</a></b></td></tr>
</table></div>
<a name="public-functions"></a>
<h2 id="public-functions">Public <span lang="zh-cn">函数</span></h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#QSettings">QSettings</a></b>(const QString &amp;<i>organization</i>, const QString &amp;<i>application</i> = QString(), QObject *<i>parent</i> = nullptr)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#QSettings-1">QSettings</a></b>(QSettings::Scope <i>scope</i>, const QString &amp;<i>organization</i>, const QString &amp;<i>application</i> = QString(), QObject *<i>parent</i> = nullptr)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#QSettings-2">QSettings</a></b>(QSettings::Format <i>format</i>, QSettings::Scope <i>scope</i>, const QString &amp;<i>organization</i>, const QString &amp;<i>application</i> = QString(), QObject *<i>parent</i> = nullptr)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#QSettings-3">QSettings</a></b>(const QString &amp;<i>fileName</i>, QSettings::Format <i>format</i>, QObject *<i>parent</i> = nullptr)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#QSettings-4">QSettings</a></b>(QObject *<i>parent</i> = nullptr)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#dtor.QSettings">~QSettings</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QStringList </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#allKeys">allKeys</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QString </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#applicationName">applicationName</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#beginGroup">beginGroup</a></b>(const QString &amp;<i>prefix</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#beginReadArray">beginReadArray</a></b>(const QString &amp;<i>prefix</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#beginWriteArray">beginWriteArray</a></b>(const QString &amp;<i>prefix</i>, int <i>size</i> = -1)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QStringList </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#childGroups">childGroups</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QStringList </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#childKeys">childKeys</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#clear">clear</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#contains">contains</a></b>(const QString &amp;<i>key</i>) const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#endArray">endArray</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#endGroup">endGroup</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#fallbacksEnabled">fallbacksEnabled</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QString </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#fileName">fileName</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QSettings::Format </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#format">format</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QString </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#group">group</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QTextCodec *</td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#iniCodec">iniCodec</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#isAtomicSyncRequired">isAtomicSyncRequired</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#isWritable">isWritable</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QString </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#organizationName">organizationName</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#remove">remove</a></b>(const QString &amp;<i>key</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QSettings::Scope </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#scope">scope</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#setArrayIndex">setArrayIndex</a></b>(int <i>i</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#setAtomicSyncRequired">setAtomicSyncRequired</a></b>(bool <i>enable</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#setFallbacksEnabled">setFallbacksEnabled</a></b>(bool <i>b</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#setIniCodec">setIniCodec</a></b>(QTextCodec *<i>codec</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#setIniCodec-1">setIniCodec</a></b>(const char *<i>codecName</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#setValue">setValue</a></b>(const QString &amp;<i>key</i>, const QVariant &amp;<i>value</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QSettings::Status </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#status">status</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#sync">sync</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QVariant </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#value">value</a></b>(const QString &amp;<i>key</i>, const QVariant &amp;<i>defaultValue</i> = QVariant()) const</td></tr>
</table></div>
<ul>
<li class="fn">32 个公用函数继承自<a href="qobject.html#public-functions">QObject</a></li>
</ul>
<a name="static-public-members"></a>
<h2 id="static-public-members">Static Public<span lang="zh-cn">成员</span></h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> QSettings::Format </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#defaultFormat">defaultFormat</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QSettings::Format </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#registerFormat">registerFormat</a></b>(const QString &amp;<i>extension</i>, QSettings::ReadFunc <i>readFunc</i>, QSettings::WriteFunc <i>writeFunc</i>, Qt::CaseSensitivity <i>caseSensitivity</i> = Qt::CaseSensitive)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#setDefaultFormat">setDefaultFormat</a></b>(QSettings::Format <i>format</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#setPath">setPath</a></b>(QSettings::Format <i>format</i>, QSettings::Scope <i>scope</i>, const QString &amp;<i>path</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> const QMetaObject </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#staticMetaObject-var">staticMetaObject</a></b></td></tr>
</table></div>
	<ul>
		<li class="fn">继承自<a href="qobject.html#static-public-members">QObject</a>的10个静态公共成员<span lang="zh-cn">。</span></li>
</ul>
<a name="reimplemented-protected-functions"></a>
<h2 id="reimplemented-protected-functions">重新实现保护Functions</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> virtual bool </td><td class="memItemRight bottomAlign"><b><a href="qsettings.html#event">event</a></b>(QEvent *<i>event</i>) override</td></tr>
</table></div>
<ul>
<li class="fn">继承自<a href="qobject.html#protected-functions">QObject</a>的9个受保护的函数<span lang="zh-cn">。</span></li>
</ul>
<h3>额外的继承成员</h3>
<ul>
<li class="fn"><span lang="zh-cn">1个</span>继承自QObject <a href="qobject.html#properties">QObject</a>的属性</li>
<li class="fn">1<span lang="zh-cn">个</span>继承<span lang="zh-cn">自</span><a href="qobject.html#public-slots">QObject</a>的公共槽</li>
<li class="fn">2个继承自<a href="qobject.html#signals">QObject</a><span lang="zh-cn">的</span>信号</li>
<li class="fn"><span lang="zh-cn">9个</span>继承自<a href="qobject.html#protected-functions">Object</a>的9个受保护的函数</li>
</ul>
<a name="details"></a>
<!-- $$$QSettings-description -->
<div class="descr">
<h2 id="details"><span lang="zh-cn">详细描述</span></h2>
<p><a href="qsettings.html">QSettings</a> 类提供持久的独立于平台的应用程序设置。</p>
<p>用户通常希望能够记住几次应用程序会话之间的设置(窗口大小、位置、选项等)。这些信息通常存储在Windows的系统注册表中，以及<a href="../qtdoc/internationalization.html#macos">macOS</a> 
和iOS的属性列表文件中。在Unix系统上，由于缺乏标准，许多应用程序(包括KDE应用程序)使用INI文本文件。</p>
<p><a href="qsettings.html">QSettings</a> 
是这些技术的一个抽象，使您能够以一种可移植的方式保存和恢复应用程序设置。它还支持<a href="qsettings.html#registerFormat">自定义存储格式</a><span lang="zh-cn">。</span></p>
<p><a href="qsettings.html">QSettings</a> 的API基于<a href="qvariant.html">QVariant</a>，允许您以最少的工作量保存大多数基于值的类型，比如<a href="qstring.html">QString</a>,<span lang="zh-cn">、</span><a href="qrect.html">QRect</a><span lang="zh-cn">以及</span><a href="../qtgui/qimage.html">QImage</a><span lang="zh-cn">。</span></p>
<p>如果您只需要一个非持久性的基于内存的设置，那么可以考虑使用 <a href="qmap.html">QMap</a>&lt;<a href="qstring.html">QString</a>, <a href="qvariant.html">QVariant</a>&gt; 
来代替。</p>
<a name="basic-usage"></a>
<h3 ><span lang="zh-cn">基本用法</span></h3>
<p>创建<a href="qsettings.html">QSettings</a>对象时，必须传递公司或组织的名称以及应用程序的名称。例如，如果您的产品名为Star 
Runner，而您的公司名为MySoft，那么您可能构建如下的 <a href="qsettings.html">QSettings</a>对象: s:</p>
<pre class="cpp">

      <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings(<span class="string">&quot;MySoft&quot;</span><span class="operator">,</span> <span class="string">&quot;Star Runner&quot;</span>);

</pre>
<p><a href="qsettings.html">QSettings</a> objects can be created either on the stack or on the heap (i.e&#x2e; using <code>new</code>). Constructing and destroying a <a href="qsettings.html">QSettings</a> object is very fast.</p>
<p>If you use <a href="qsettings.html">QSettings</a> from many places in your application, you might want to specify the organization name and the application name using <a href="qcoreapplication.html#organizationName-prop">QCoreApplication::setOrganizationName</a>() and <a href="qcoreapplication.html#applicationName-prop">QCoreApplication::setApplicationName</a>(), and then use the default <a href="qsettings.html">QSettings</a> constructor:</p>
<pre class="cpp">

      <span class="type"><a href="qcoreapplication.html">QCoreApplication</a></span><span class="operator">::</span>setOrganizationName(<span class="string">&quot;MySoft&quot;</span>);
      <span class="type"><a href="qcoreapplication.html">QCoreApplication</a></span><span class="operator">::</span>setOrganizationDomain(<span class="string">&quot;mysoft.com&quot;</span>);
      <span class="type"><a href="qcoreapplication.html">QCoreApplication</a></span><span class="operator">::</span>setApplicationName(<span class="string">&quot;Star Runner&quot;</span>);
      ...
      <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings;

</pre>
<p>(Here, we also specify the organization's Internet domain. When the Internet domain is set, it is used on <a href="../qtdoc/internationalization.html#macos">macOS</a> and iOS instead of the organization name, since <a href="../qtdoc/internationalization.html#macos">macOS</a> and iOS applications conventionally use Internet domains to identify themselves. If no domain is set, a fake domain is derived from the organization name. See the <a href="qsettings.html#platform-specific-notes">Platform-Specific Notes</a> below for details.)</p>
<p><a href="qsettings.html">QSettings</a> stores settings. Each setting consists of a <a href="qstring.html">QString</a> that specifies the setting's name (the <i>key</i>) and a <a href="qvariant.html">QVariant</a> that stores the data associated with the key. To write a setting, use <a href="qsettings.html#setValue">setValue</a>(). For example:</p>
<pre class="cpp">

      settings<span class="operator">.</span>setValue(<span class="string">&quot;editor/wrapMargin&quot;</span><span class="operator">,</span> <span class="number">68</span>);

</pre>
<p>If there already exists a setting with the same key, the existing value is overwritten by the new value. For efficiency, the changes may not be saved to permanent storage immediately. (You can always call <a href="qsettings.html#sync">sync</a>() to commit your changes.)</p>
<p>You can get a setting's value back using <a href="qsettings.html#value">value</a>():</p>
<pre class="cpp">

      <span class="type">int</span> margin <span class="operator">=</span> settings<span class="operator">.</span>value(<span class="string">&quot;editor/wrapMargin&quot;</span>)<span class="operator">.</span>toInt();

</pre>
<p>If there is no setting with the specified name, <a href="qsettings.html">QSettings</a> returns a null <a href="qvariant.html">QVariant</a> (which can be converted to the integer 0). You can specify another default value by passing a second argument to <a href="qsettings.html#value">value</a>():</p>
<pre class="cpp">

      <span class="type">int</span> margin <span class="operator">=</span> settings<span class="operator">.</span>value(<span class="string">&quot;editor/wrapMargin&quot;</span><span class="operator">,</span> <span class="number">80</span>)<span class="operator">.</span>toInt();

</pre>
<p>To test whether a given key exists, call <a href="qsettings.html#contains">contains</a>(). To remove the setting associated with a key, call <a href="qsettings.html#remove">remove</a>(). To obtain the list of all keys, call <a href="qsettings.html#allKeys">allKeys</a>(). To remove all keys, call <a href="qsettings.html#clear">clear</a>().</p>
<a name="qvariant-and-gui-types"></a>
<h3 >QVariant and GUI Types</h3>
<p>Because <a href="qvariant.html">QVariant</a> is part of the Qt Core module, it cannot provide conversion functions to data types such as <a href="../qtgui/qcolor.html">QColor</a>, <a href="../qtgui/qimage.html">QImage</a>, and <a href="../qtgui/qpixmap.html">QPixmap</a>, which are part of Qt GUI. In other words, there is no <code>toColor()</code>, <code>toImage()</code>, or <code>toPixmap()</code> functions in <a href="qvariant.html">QVariant</a>.</p>
<p>Instead, you can use the <a href="qvariant.html#value">QVariant::value</a>() or the qVariantValue() template function. For example:</p>
<pre class="cpp">

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings(<span class="string">&quot;MySoft&quot;</span><span class="operator">,</span> <span class="string">&quot;Star Runner&quot;</span>);
  <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color <span class="operator">=</span> settings<span class="operator">.</span>value(<span class="string">&quot;DataPump/bgcolor&quot;</span>)<span class="operator">.</span>value<span class="operator">&lt;</span><span class="type"><a href="../qtgui/qcolor.html">QColor</a></span><span class="operator">&gt;</span>();

</pre>
<p>The inverse conversion (e.g&#x2e;, from <a href="../qtgui/qcolor.html">QColor</a> to <a href="qvariant.html">QVariant</a>) is automatic for all data types supported by <a href="qvariant.html">QVariant</a>, including GUI-related types:</p>
<pre class="cpp">

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings(<span class="string">&quot;MySoft&quot;</span><span class="operator">,</span> <span class="string">&quot;Star Runner&quot;</span>);
  <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color <span class="operator">=</span> palette()<span class="operator">.</span>background()<span class="operator">.</span>color();
  settings<span class="operator">.</span>setValue(<span class="string">&quot;DataPump/bgcolor&quot;</span><span class="operator">,</span> color);

</pre>
<p>Custom types registered using <a href="qmetatype.html#qRegisterMetaType-1">qRegisterMetaType</a>() and <a href="qmetatype.html#qRegisterMetaTypeStreamOperators">qRegisterMetaTypeStreamOperators</a>() can be stored using <a href="qsettings.html">QSettings</a>.</p>
<a name="section-and-key-syntax"></a>
<h3 >Section and Key Syntax</h3>
<p>Setting keys can contain any Unicode characters. The Windows registry and INI files use case-insensitive keys, whereas the CFPreferences API on <a href="../qtdoc/internationalization.html#macos">macOS</a> and iOS uses case-sensitive keys. To avoid portability problems, follow these simple rules:</p>
<ol class="1" type="1"><li>Always refer to the same key using the same case. For example, if you refer to a key as &quot;text fonts&quot; in one place in your code, don't refer to it as &quot;Text Fonts&quot; somewhere else.</li>
<li>Avoid key names that are identical except for the case. For example, if you have a key called &quot;<a href="../qtdesigner/designer-to-know.html#mainwindow">MainWindow</a>&quot;, don't try to save another key as &quot;mainwindow&quot;.</li>
<li>Do not use slashes ('/' and '\') in section or key names; the backslash character is used to separate sub keys (see below). On windows '\' are converted by <a href="qsettings.html">QSettings</a> to '/', which makes them identical.</li>
</ol>
<p>You can form hierarchical keys using the '/' character as a separator, similar to Unix file paths. For example:</p>
<pre class="cpp">

      settings<span class="operator">.</span>setValue(<span class="string">&quot;mainwindow/size&quot;</span><span class="operator">,</span> win<span class="operator">-</span><span class="operator">&gt;</span>size());
      settings<span class="operator">.</span>setValue(<span class="string">&quot;mainwindow/fullScreen&quot;</span><span class="operator">,</span> win<span class="operator">-</span><span class="operator">&gt;</span>isFullScreen());
      settings<span class="operator">.</span>setValue(<span class="string">&quot;outputpanel/visible&quot;</span><span class="operator">,</span> panel<span class="operator">-</span><span class="operator">&gt;</span>isVisible());

</pre>
<p>If you want to save or restore many settings with the same prefix, you can specify the prefix using <a href="qsettings.html#beginGroup">beginGroup</a>() and call <a href="qsettings.html#endGroup">endGroup</a>() at the end. Here's the same example again, but this time using the group mechanism:</p>
<pre class="cpp">

      settings<span class="operator">.</span>beginGroup(<span class="string">&quot;mainwindow&quot;</span>);
      settings<span class="operator">.</span>setValue(<span class="string">&quot;size&quot;</span><span class="operator">,</span> win<span class="operator">-</span><span class="operator">&gt;</span>size());
      settings<span class="operator">.</span>setValue(<span class="string">&quot;fullScreen&quot;</span><span class="operator">,</span> win<span class="operator">-</span><span class="operator">&gt;</span>isFullScreen());
      settings<span class="operator">.</span>endGroup();

      settings<span class="operator">.</span>beginGroup(<span class="string">&quot;outputpanel&quot;</span>);
      settings<span class="operator">.</span>setValue(<span class="string">&quot;visible&quot;</span><span class="operator">,</span> panel<span class="operator">-</span><span class="operator">&gt;</span>isVisible());
      settings<span class="operator">.</span>endGroup();

</pre>
<p>If a group is set using <a href="qsettings.html#beginGroup">beginGroup</a>(), the behavior of most functions changes consequently. Groups can be set recursively.</p>
<p>In addition to groups, <a href="qsettings.html">QSettings</a> also supports an &quot;array&quot; concept. See <a href="qsettings.html#beginReadArray">beginReadArray</a>() and <a href="qsettings.html#beginWriteArray">beginWriteArray</a>() for details.</p>
<a name="fallback-mechanism"></a>
<h3 >Fallback Mechanism</h3>
<p>Let's assume that you have created a <a href="qsettings.html">QSettings</a> object with the organization name MySoft and the application name Star Runner. When you look up a value, up to four locations are searched in that order:</p>
<ol class="1" type="1"><li>a user-specific location for the Star Runner application</li>
<li>a user-specific location for all applications by MySoft</li>
<li>a system-wide location for the Star Runner application</li>
<li>a system-wide location for all applications by MySoft</li>
</ol>
<p>(See <a href="qsettings.html#platform-specific-notes">Platform-Specific Notes</a> below for information on what these locations are on the different platforms supported by Qt.)</p>
<p>If a key cannot be found in the first location, the search goes on in the second location, and so on. This enables you to store system-wide or organization-wide settings and to override them on a per-user or per-application basis. To turn off this mechanism, call <a href="qsettings.html#setFallbacksEnabled">setFallbacksEnabled</a>(false).</p>
<p>Although keys from all four locations are available for reading, only the first file (the user-specific location for the application at hand) is accessible for writing. To write to any of the other files, omit the application name and/or specify <a href="qsettings.html#Scope-enum">QSettings::SystemScope</a> (as opposed to <a href="qsettings.html#Scope-enum">QSettings::UserScope</a>, the default).</p>
<p>Let's see with an example:</p>
<pre class="cpp">

      <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> obj1(<span class="string">&quot;MySoft&quot;</span><span class="operator">,</span> <span class="string">&quot;Star Runner&quot;</span>);
      <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> obj2(<span class="string">&quot;MySoft&quot;</span>);
      <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> obj3(<span class="type"><a href="qsettings.html#QSettings">QSettings</a></span><span class="operator">::</span>SystemScope<span class="operator">,</span> <span class="string">&quot;MySoft&quot;</span><span class="operator">,</span> <span class="string">&quot;Star Runner&quot;</span>);
      <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> obj4(<span class="type"><a href="qsettings.html#QSettings">QSettings</a></span><span class="operator">::</span>SystemScope<span class="operator">,</span> <span class="string">&quot;MySoft&quot;</span>);

</pre>
<p>The table below summarizes which <a href="qsettings.html">QSettings</a> objects access which location. &quot;<b>X</b>&quot; means that the location is the main location associated to the <a href="qsettings.html">QSettings</a> object and is used both for reading and for writing; &quot;o&quot; means that the location is used as a fallback when reading.</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><th >Locations</th><th ><code>obj1</code></th><th ><code>obj2</code></th><th ><code>obj3</code></th><th ><code>obj4</code></th></tr></thead>
<tr valign="top" class="odd"><td >1. User, Application</td><td ><b>X</b></td><td ></td><td ></td><td ></td></tr>
<tr valign="top" class="even"><td >2. User, Organization</td><td >o</td><td ><b>X</b></td><td ></td><td ></td></tr>
<tr valign="top" class="odd"><td >3. System, Application</td><td >o</td><td ></td><td ><b>X</b></td><td ></td></tr>
<tr valign="top" class="even"><td >4. System, Organization</td><td >o</td><td >o</td><td >o</td><td ><b>X</b></td></tr>
</table></div>
<p>The beauty of this mechanism is that it works on all platforms supported by Qt and that it still gives you a lot of flexibility, without requiring you to specify any file names or registry paths.</p>
<p>If you want to use INI files on all platforms instead of the native API, you can pass <a href="qsettings.html#Format-enum">QSettings::IniFormat</a> as the first argument to the <a href="qsettings.html">QSettings</a> constructor, followed by the scope, the organization name, and the application name:</p>
<pre class="cpp">

      <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings(<span class="type"><a href="qsettings.html#QSettings">QSettings</a></span><span class="operator">::</span>IniFormat<span class="operator">,</span> <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span><span class="operator">::</span>UserScope<span class="operator">,</span>
                         <span class="string">&quot;MySoft&quot;</span><span class="operator">,</span> <span class="string">&quot;Star Runner&quot;</span>);

</pre>
<p>Note that type information is not preserved when reading settings from INI files; all values will be returned as <a href="qstring.html">QString</a>.</p>
<p>The <a href="../qtwidgets/qtwidgets-tools-settingseditor-example.html">Settings Editor</a> example lets you experiment with different settings location and with fallbacks turned on or off.</p>
<a name="restoring-the-state-of-a-gui-application"></a>
<h3 >Restoring the State of a GUI Application</h3>
<p><a href="qsettings.html">QSettings</a> is often used to store the state of a GUI application. The following example illustrates how to use <a href="qsettings.html">QSettings</a> to save and restore the geometry of an application's main window.</p>
<pre class="cpp">

  <span class="type">void</span> MainWindow<span class="operator">::</span>writeSettings()
  {
      <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings(<span class="string">&quot;Moose Soft&quot;</span><span class="operator">,</span> <span class="string">&quot;Clipper&quot;</span>);

      settings<span class="operator">.</span>beginGroup(<span class="string">&quot;MainWindow&quot;</span>);
      settings<span class="operator">.</span>setValue(<span class="string">&quot;size&quot;</span><span class="operator">,</span> size());
      settings<span class="operator">.</span>setValue(<span class="string">&quot;pos&quot;</span><span class="operator">,</span> pos());
      settings<span class="operator">.</span>endGroup();
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>readSettings()
  {
      <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings(<span class="string">&quot;Moose Soft&quot;</span><span class="operator">,</span> <span class="string">&quot;Clipper&quot;</span>);

      settings<span class="operator">.</span>beginGroup(<span class="string">&quot;MainWindow&quot;</span>);
      resize(settings<span class="operator">.</span>value(<span class="string">&quot;size&quot;</span><span class="operator">,</span> <span class="type"><a href="qsize.html">QSize</a></span>(<span class="number">400</span><span class="operator">,</span> <span class="number">400</span>))<span class="operator">.</span>toSize());
      move(settings<span class="operator">.</span>value(<span class="string">&quot;pos&quot;</span><span class="operator">,</span> <span class="type"><a href="qpoint.html">QPoint</a></span>(<span class="number">200</span><span class="operator">,</span> <span class="number">200</span>))<span class="operator">.</span>toPoint());
      settings<span class="operator">.</span>endGroup();
  }

</pre>
<p>See <a href="../qtwidgets/application-windows.html#window-geometry">Window Geometry</a> for a discussion on why it is better to call <a href="../qtwidgets/qwidget.html#size-prop">QWidget::resize</a>() and <a href="../qtwidgets/qwidget.html#pos-prop">QWidget::move</a>() rather than <a href="../qtwidgets/qwidget.html#geometry-prop">QWidget::setGeometry</a>() to restore a window's geometry.</p>
<p>The <code>readSettings()</code> and <code>writeSettings()</code> functions must be called from the main window's constructor and close event handler as follows:</p>
<pre class="cpp">

  MainWindow<span class="operator">::</span>MainWindow()
  {
      ...
      readSettings();
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>closeEvent(<span class="type"><a href="../qtgui/qcloseevent.html">QCloseEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (userReallyWantsToQuit()) {
          writeSettings();
          event<span class="operator">-</span><span class="operator">&gt;</span>accept();
      } <span class="keyword">else</span> {
          event<span class="operator">-</span><span class="operator">&gt;</span>ignore();
      }
  }

</pre>
<p>See the <a href="../qtwidgets/qtwidgets-mainwindows-application-example.html">Application</a> example for a self-contained example that uses <a href="qsettings.html">QSettings</a>.</p>
<a name="accessing-settings-from-multiple-threads-or-processes-simultaneously"></a>
<h3 >Accessing Settings from Multiple Threads or Processes Simultaneously</h3>
<p><a href="qsettings.html">QSettings</a> is <a href="../qtdoc/threads-reentrancy.html">reentrant</a>. This means that you can use distinct <a href="qsettings.html">QSettings</a> object in different threads simultaneously. This guarantee stands even when the <a href="qsettings.html">QSettings</a> objects refer to the same files on disk (or to the same entries in the system registry). If a setting is modified through one <a href="qsettings.html">QSettings</a> object, the change will immediately be visible in any other <a href="qsettings.html">QSettings</a> objects that operate on the same location and that live in the same process.</p>
<p><a href="qsettings.html">QSettings</a> can safely be used from different processes (which can be different instances of your application running at the same time or different applications altogether) to read and write to the same system locations, provided certain conditions are met. For <a href="qsettings.html#Format-enum">QSettings::IniFormat</a>, it uses advisory file locking and a smart merging algorithm to ensure data integrity. The condition for that to work is that the writeable configuration file must be a regular file and must reside in a directory that the current user can create new, temporary files in. If that is not the case, then one must use <a href="qsettings.html#setAtomicSyncRequired">setAtomicSyncRequired</a>() to turn the safety off.</p>
<p>Note that <a href="qsettings.html#sync">sync</a>() imports changes made by other processes (in addition to writing the changes from this <a href="qsettings.html">QSettings</a>).</p>
<a name="platform-specific-notes"></a>
<h3 >Platform-Specific Notes</h3>
<a name="locations-where-application-settings-are-stored"></a>
<h4 >Locations Where Application Settings Are Stored</h4>
<p>As mentioned in the <a href="qsettings.html#fallback-mechanism">Fallback Mechanism</a> section, <a href="qsettings.html">QSettings</a> stores settings for an application in up to four locations, depending on whether the settings are user-specific or system-wide and whether the settings are application-specific or organization-wide. For simplicity, we're assuming the organization is called MySoft and the application is called Star Runner.</p>
<p>On Unix systems, if the file format is <a href="qsettings.html#Format-enum">NativeFormat</a>, the following files are used by default:</p>
<ol class="1" type="1"><li><code>$HOME/.config/MySoft/Star Runner.conf</code> (Qt for Embedded Linux: <code>$HOME/Settings/MySoft/Star Runner.conf</code>)</li>
<li><code>$HOME/.config/MySoft.conf</code> (Qt for Embedded Linux: <code>$HOME/Settings/MySoft.conf</code>)</li>
<li>for each directory &lt;dir&gt; in $XDG_CONFIG_DIRS: <code>&lt;dir&gt;/MySoft/Star Runner.conf</code></li>
<li>for each directory &lt;dir&gt; in $XDG_CONFIG_DIRS: <code>&lt;dir&gt;/MySoft.conf</code></li>
</ol>
<p><b>Note: </b>If XDG_CONFIG_DIRS is unset, the default value of <code>/etc/xdg</code> is used.</p><p>On <a href="../qtdoc/internationalization.html#macos">macOS</a> versions 10.2 and 10.3, these files are used by default:</p>
<ol class="1" type="1"><li><code>$HOME/Library/Preferences/com.MySoft.Star Runner.plist</code></li>
<li><code>$HOME/Library/Preferences/com.MySoft.plist</code></li>
<li><code>/Library/Preferences/com.MySoft.Star Runner.plist</code></li>
<li><code>/Library/Preferences/com.MySoft.plist</code></li>
</ol>
<p>On Windows, <a href="qsettings.html#Format-enum">NativeFormat</a> settings are stored in the following registry paths:</p>
<ol class="1" type="1"><li><code>HKEY_CURRENT_USER\Software\MySoft\Star Runner</code></li>
<li><code>HKEY_CURRENT_USER\Software\MySoft\OrganizationDefaults</code></li>
<li><code>HKEY_LOCAL_MACHINE\Software\MySoft\Star Runner</code></li>
<li><code>HKEY_LOCAL_MACHINE\Software\MySoft\OrganizationDefaults</code></li>
</ol>
<p><b>Note: </b>On Windows, for 32-bit programs running in WOW64 mode, settings are stored in the following registry path: <code>HKEY_LOCAL_MACHINE\Software\WOW6432node</code>.</p><p>If the file format is <a href="qsettings.html#Format-enum">NativeFormat</a>, this is &quot;Settings/MySoft/Star Runner.conf&quot; in the application's home directory.</p>
<p>If the file format is <a href="qsettings.html#Format-enum">IniFormat</a>, the following files are used on Unix, <a href="../qtdoc/internationalization.html#macos">macOS</a>, and iOS:</p>
<ol class="1" type="1"><li><code>$HOME/.config/MySoft/Star Runner.ini</code> (Qt for Embedded Linux: <code>$HOME/Settings/MySoft/Star Runner.ini</code>)</li>
<li><code>$HOME/.config/MySoft.ini</code> (Qt for Embedded Linux: <code>$HOME/Settings/MySoft.ini</code>)</li>
<li>for each directory &lt;dir&gt; in $XDG_CONFIG_DIRS: <code>&lt;dir&gt;/MySoft/Star Runner.ini</code></li>
<li>for each directory &lt;dir&gt; in $XDG_CONFIG_DIRS: <code>&lt;dir&gt;/MySoft.ini</code></li>
</ol>
<p><b>Note: </b>If XDG_CONFIG_DIRS is unset, the default value of <code>/etc/xdg</code> is used.</p><p>On Windows, the following files are used:</p>
<ol class="1" type="1"><li><code>FOLDERID_RoamingAppData\MySoft\Star Runner.ini</code></li>
<li><code>FOLDERID_RoamingAppData\MySoft.ini</code></li>
<li><code>FOLDERID_ProgramData\MySoft\Star Runner.ini</code></li>
<li><code>FOLDERID_ProgramData\MySoft.ini</code></li>
</ol>
<p>The identifiers prefixed by <code>FOLDERID_</code> are special item ID lists to be passed to the Win32 API function <code>SHGetKnownFolderPath()</code> to obtain the corresponding path.</p>
<p><code>FOLDERID_RoamingAppData</code> usually points to <code>C:\Users\<i>User Name</i>\AppData\Roaming</code>, also shown by the environment variable <code>%APPDATA%</code>.</p>
<p><code>FOLDERID_ProgramData</code> usually points to <code>C:\ProgramData</code>.</p>
<p>If the file format is <a href="qsettings.html#Format-enum">IniFormat</a>, this is &quot;Settings/MySoft/Star Runner.ini&quot; in the application's home directory.</p>
<p>The paths for the <code>.ini</code> and <code>.conf</code> files can be changed using <a href="qsettings.html#setPath">setPath</a>(). On Unix, <a href="../qtdoc/internationalization.html#macos">macOS</a>, and iOS the user can override them by setting the <code>XDG_CONFIG_HOME</code> environment variable; see <a href="qsettings.html#setPath">setPath</a>() for details.</p>
<a name="accessing-ini-and-plist-files-directly"></a>
<h4 >Accessing INI and .plist Files Directly</h4>
<p>Sometimes you do want to access settings stored in a specific file or registry path. On all platforms, if you want to read an INI file directly, you can use the <a href="qsettings.html">QSettings</a> constructor that takes a file name as first argument and pass <a href="qsettings.html#Format-enum">QSettings::IniFormat</a> as second argument. For example:</p>
<pre class="cpp">

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings(<span class="string">&quot;/home/petra/misc/myapp.ini&quot;</span><span class="operator">,</span>
                     <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span><span class="operator">::</span>IniFormat);

</pre>
<p>You can then use the <a href="qsettings.html">QSettings</a> object to read and write settings in the file.</p>
<p>On <a href="../qtdoc/internationalization.html#macos">macOS</a> and iOS, you can access property list <code>.plist</code> files by passing <a href="qsettings.html#Format-enum">QSettings::NativeFormat</a> as second argument. For example:</p>
<pre class="cpp">

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings(<span class="string">&quot;/Users/petra/misc/myapp.plist&quot;</span><span class="operator">,</span>
                     <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span><span class="operator">::</span>NativeFormat);

</pre>
<a name="accessing-the-windows-registry-directly"></a>
<h4 >Accessing the Windows Registry Directly</h4>
<p>On Windows, <a href="qsettings.html">QSettings</a> lets you access settings that have been written with <a href="qsettings.html">QSettings</a> (or settings in a supported format, e.g&#x2e;, string data) in the system registry. This is done by constructing a <a href="qsettings.html">QSettings</a> object with a path in the registry and <a href="qsettings.html#Format-enum">QSettings::NativeFormat</a>.</p>
<p>For example:</p>
<pre class="cpp">

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings(<span class="string">&quot;HKEY_CURRENT_USER\\Software\\Microsoft\\Office&quot;</span><span class="operator">,</span>
                     <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span><span class="operator">::</span>NativeFormat);

</pre>
<p>All the registry entries that appear under the specified path can be read or written through the <a href="qsettings.html">QSettings</a> object as usual (using forward slashes instead of backslashes). For example:</p>
<pre class="cpp">

  settings<span class="operator">.</span>setValue(<span class="string">&quot;11.0/Outlook/Security/DontTrustInstalledFiles&quot;</span><span class="operator">,</span> <span class="number">0</span>);

</pre>
<p>Note that the backslash character is, as mentioned, used by <a href="qsettings.html">QSettings</a> to separate subkeys. As a result, you cannot read or write windows registry entries that contain slashes or backslashes; you should use a native windows API if you need to do so.</p>
<a name="accessing-common-registry-settings-on-windows"></a>
<h4 >Accessing Common Registry Settings on Windows</h4>
<p>On Windows, it is possible for a key to have both a value and subkeys. Its default value is accessed by using &quot;Default&quot; or &quot;.&quot; in place of a subkey:</p>
<pre class="cpp">

  settings<span class="operator">.</span>setValue(<span class="string">&quot;HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy&quot;</span><span class="operator">,</span> <span class="string">&quot;Milkyway&quot;</span>);
  settings<span class="operator">.</span>setValue(<span class="string">&quot;HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy\\Sun&quot;</span><span class="operator">,</span> <span class="string">&quot;OurStar&quot;</span>);
  settings<span class="operator">.</span>value(<span class="string">&quot;HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy\\Default&quot;</span>); <span class="comment">// returns &quot;Milkyway&quot;</span>

</pre>
<p>On other platforms than Windows, &quot;Default&quot; and &quot;.&quot; would be treated as regular subkeys.</p>
<a name="platform-limitations"></a>
<h4 >Platform Limitations</h4>
<p>While <a href="qsettings.html">QSettings</a> attempts to smooth over the differences between the different supported platforms, there are still a few differences that you should be aware of when porting your application:</p>
<ul>
<li>The Windows system registry has the following limitations: A subkey may not exceed 255 characters, an entry's value may not exceed 16,383 characters, and all the values of a key may not exceed 65,535 characters. One way to work around these limitations is to store the settings using the <a href="qsettings.html#Format-enum">IniFormat</a> instead of the <a href="qsettings.html#Format-enum">NativeFormat</a>.</li>
<li>On Windows, when the Windows system registry is used, <a href="qsettings.html">QSettings</a> does not preserve the original type of the value. Therefore, the type of the value might change when a new value is set. For example, a value with type <code>REG_EXPAND_SZ</code> will change to <code>REG_SZ</code>.</li>
<li>On <a href="../qtdoc/internationalization.html#macos">macOS</a> and iOS, <a href="qsettings.html#allKeys">allKeys</a>() will return some extra keys for global settings that apply to all applications. These keys can be read using <a href="qsettings.html#value">value</a>() but cannot be changed, only shadowed. Calling <a href="qsettings.html#setFallbacksEnabled">setFallbacksEnabled</a>(false) will hide these global settings.</li>
<li>On <a href="../qtdoc/internationalization.html#macos">macOS</a> and iOS, the CFPreferences API used by <a href="qsettings.html">QSettings</a> expects Internet domain names rather than organization names. To provide a uniform API, <a href="qsettings.html">QSettings</a> derives a fake domain name from the organization name (unless the organization name already is a domain name, e.g&#x2e; OpenOffice.org). The algorithm appends &quot;.com&quot; to the company name and replaces spaces and other illegal characters with hyphens. If you want to specify a different domain name, call <a href="qcoreapplication.html#organizationDomain-prop">QCoreApplication::setOrganizationDomain</a>(), <a href="qcoreapplication.html#organizationName-prop">QCoreApplication::setOrganizationName</a>(), and <a href="qcoreapplication.html#applicationName-prop">QCoreApplication::setApplicationName</a>() in your <code>main()</code> function and then use the default <a href="qsettings.html">QSettings</a> constructor. Another solution is to use preprocessor directives, for example:<pre class="cpp">

  <span class="preprocessor">#ifdef Q_OS_MAC</span>
      <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings(<span class="string">&quot;grenoullelogique.fr&quot;</span><span class="operator">,</span> <span class="string">&quot;Squash&quot;</span>);
  <span class="preprocessor">#else</span>
      <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings(<span class="string">&quot;Grenoulle Logique&quot;</span><span class="operator">,</span> <span class="string">&quot;Squash&quot;</span>);
  <span class="preprocessor">#endif</span>

</pre>
</li>
<li>On <a href="../qtdoc/internationalization.html#macos">macOS</a>, permissions to access settings not belonging to the current user (i.e&#x2e; <a href="qsettings.html#Scope-enum">SystemScope</a>) have changed with 10.7 (Lion). Prior to that version, users having admin rights could access these. For 10.7 and 10.8 (Mountain Lion), only root can. However, 10.9 (Mavericks) changes that rule again but only for the native format (plist files).</li>
</ul>
</div>
<p><b>请参考</b> <a href="qvariant.html">QVariant</a>，<a href="../qtgui/qsessionmanager.html">QSessionManager</a>，<a href="../qtwidgets/qtwidgets-tools-settingseditor-example.html">Settings Editor Example</a> 和 <a href="../qtwidgets/qtwidgets-mainwindows-application-example.html">Application Example</a>。</p>
<!-- @@@QSettings -->
<div class="types">
<h2>成员类型文档</h2>
<!-- $$$Format$$$NativeFormat$$$IniFormat$$$Registry32Format$$$Registry64Format$$$InvalidFormat$$$CustomFormat1$$$CustomFormat2$$$CustomFormat3$$$CustomFormat4$$$CustomFormat5$$$CustomFormat6$$$CustomFormat7$$$CustomFormat8$$$CustomFormat9$$$CustomFormat10$$$CustomFormat11$$$CustomFormat12$$$CustomFormat13$$$CustomFormat14$$$CustomFormat15$$$CustomFormat16 -->
<h3 class="fn" id="Format-enum"><a name="Format-enum"></a>enum QSettings::<span class="name">Format</span></h3>
<p>This enum type specifies the storage format used by <a href="qsettings.html">QSettings</a>.</p>
<div class="table"><table class="valuelist"><tr valign="top" class="odd"><th class="tblConst">Constant</th><th class="tblval">Value</th><th class="tbldscr">Description</th></tr>
<tr><td class="topAlign"><code>QSettings::NativeFormat</code></td><td class="topAlign tblval"><code>0</code></td><td class="topAlign">Store the settings using the most appropriate storage format for the platform. On Windows, this means the system registry; on <a href="../qtdoc/internationalization.html#macos">macOS</a> and iOS, this means the CFPreferences API; on Unix, this means textual configuration files in INI format.</td></tr>
<tr><td class="topAlign"><code>QSettings::Registry32Format</code></td><td class="topAlign tblval"><code>2</code></td><td class="topAlign">Windows only: Explicitly access the 32-bit system registry from a 64-bit application running on 64-bit Windows. On 32-bit Windows or from a 32-bit application on 64-bit Windows, this works the same as specifying NativeFormat. This enum value was added in Qt 5.7&#x2e;</td></tr>
<tr><td class="topAlign"><code>QSettings::Registry64Format</code></td><td class="topAlign tblval"><code>3</code></td><td class="topAlign">Windows only: Explicitly access the 64-bit system registry from a 32-bit application running on 64-bit Windows. On 32-bit Windows or from a 64-bit application on 64-bit Windows, this works the same as specifying NativeFormat. This enum value was added in Qt 5.7&#x2e;</td></tr>
<tr><td class="topAlign"><code>QSettings::IniFormat</code></td><td class="topAlign tblval"><code>1</code></td><td class="topAlign">Store the settings in INI files. Note that type information is not preserved when reading settings from INI files; all values will be returned as <a href="qstring.html">QString</a>.</td></tr>
<tr><td class="topAlign"><code>QSettings::InvalidFormat</code></td><td class="topAlign tblval"><code>16</code></td><td class="topAlign">Special value returned by <a href="qsettings.html#registerFormat">registerFormat</a>().</td></tr>
</table></div>
<p>On Unix, NativeFormat and IniFormat mean the same thing, except that the file extension is different (<code>.conf</code> for NativeFormat, <code>.ini</code> for IniFormat).</p>
<p>The INI file format is a Windows file format that Qt supports on all platforms. In the absence of an INI standard, we try to follow what Microsoft does, with the following exceptions:</p>
<ul>
<li>If you store types that <a href="qvariant.html">QVariant</a> can't convert to <a href="qstring.html">QString</a> (e.g&#x2e;, <a href="qpoint.html">QPoint</a>, <a href="qrect.html">QRect</a>, and <a href="qsize.html">QSize</a>), Qt uses an <code>@</code>-based syntax to encode the type. For example:<pre class="cpp">

  pos <span class="operator">=</span> @Point(<span class="number">100</span> <span class="number">100</span>)

</pre>
<p>To minimize compatibility issues, any <code>@</code> that doesn't appear at the first position in the value or that isn't followed by a Qt type (<code>Point</code>, <code>Rect</code>, <code>Size</code>, etc.) is treated as a normal character.</p>
</li>
<li>Although backslash is a special character in INI files, most Windows applications don't escape backslashes (<code>\</code>) in file paths:<pre class="cpp">

  windir <span class="operator">=</span> C:\Windows

</pre>
<p><a href="qsettings.html">QSettings</a> always treats backslash as a special character and provides no API for reading or writing such entries.</p>
</li>
<li>The INI file format has severe restrictions on the syntax of a key. Qt works around this by using <code>%</code> as an escape character in keys. In addition, if you save a top-level setting (a key with no slashes in it, e.g&#x2e;, &quot;someKey&quot;), it will appear in the INI file's &quot;General&quot; section. To avoid overwriting other keys, if you save something using a key such as &quot;General/someKey&quot;, the key will be located in the &quot;%General&quot; section, <i>not</i> in the &quot;General&quot; section.</li>
<li>Following the philosophy that we should be liberal in what we accept and conservative in what we generate, <a href="qsettings.html">QSettings</a> will accept Latin-1 encoded INI files, but generate pure ASCII files, where non-ASCII values are encoded using standard INI escape sequences. To make the INI files more readable (but potentially less compatible), call <a href="qsettings.html#setIniCodec">setIniCodec</a>().</li>
</ul>
<p><b>请参考 </b><a href="qsettings.html#registerFormat">registerFormat</a>() 和 <a href="qsettings.html#setPath">setPath</a>()。</p>
<!-- @@@Format -->
<!-- $$$ReadFunc -->
<h3 class="fn" id="ReadFunc-typedef"><a name="ReadFunc-typedef"></a>typedef QSettings::<span class="name">ReadFunc</span></h3>
<p>Typedef for a pointer to a function with the following signature:</p>
<pre class="cpp">

  bool myReadFunc(<span class="type"><a href="qiodevice.html">QIODevice</a></span> <span class="operator">&amp;</span>device<span class="operator">,</span> <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span><span class="operator">::</span>SettingsMap <span class="operator">&amp;</span>map);

</pre>
<p><code>ReadFunc</code> is used in <code>registerFormat()</code> as a pointer to a function that reads a set of key/value pairs. <code>ReadFunc</code> should read all the options in one pass, and return all the settings in the <code>SettingsMap</code> container, which is initially empty.</p>
<p><b>请参考 </b><a href="qsettings.html#WriteFunc-typedef">WriteFunc</a> 和 <a href="qsettings.html#registerFormat">registerFormat</a>()。</p>
<!-- @@@ReadFunc -->
<!-- $$$Scope$$$UserScope$$$SystemScope -->
<h3 class="fn" id="Scope-enum"><a name="Scope-enum"></a>enum QSettings::<span class="name">Scope</span></h3>
<p>This enum specifies whether settings are user-specific or shared by all users of the same system.</p>
<div class="table"><table class="valuelist"><tr valign="top" class="odd"><th class="tblConst">Constant</th><th class="tblval">Value</th><th class="tbldscr">Description</th></tr>
<tr><td class="topAlign"><code>QSettings::UserScope</code></td><td class="topAlign tblval"><code>0</code></td><td class="topAlign">Store settings in a location specific to the current user (e.g&#x2e;, in the user's home directory).</td></tr>
<tr><td class="topAlign"><code>QSettings::SystemScope</code></td><td class="topAlign tblval"><code>1</code></td><td class="topAlign">Store settings in a global location, so that all users on the same machine access the same set of settings.</td></tr>
</table></div>
<p><b>请参考 </b><a href="qsettings.html#setPath">setPath</a>()。</p>
<!-- @@@Scope -->
<!-- $$$SettingsMap -->
<h3 class="fn" id="SettingsMap-typedef"><a name="SettingsMap-typedef"></a>typedef QSettings::<span class="name">SettingsMap</span></h3>
<p>Typedef for <a href="qmap.html">QMap</a>&lt;<a href="qstring.html">QString</a>, <a href="qvariant.html">QVariant</a>&gt;.</p>
<p><b>请参考 </b><a href="qsettings.html#registerFormat">registerFormat</a>()。</p>
<!-- @@@SettingsMap -->
<!-- $$$Status$$$NoError$$$AccessError$$$FormatError -->
<h3 class="fn" id="Status-enum"><a name="Status-enum"></a>enum QSettings::<span class="name">Status</span></h3>
<p>The following status values are possible:</p>
<div class="table"><table class="valuelist"><tr valign="top" class="odd"><th class="tblConst">Constant</th><th class="tblval">Value</th><th class="tbldscr">Description</th></tr>
<tr><td class="topAlign"><code>QSettings::NoError</code></td><td class="topAlign tblval"><code>0</code></td><td class="topAlign">No error occurred.</td></tr>
<tr><td class="topAlign"><code>QSettings::AccessError</code></td><td class="topAlign tblval"><code>1</code></td><td class="topAlign">An access error occurred (e.g&#x2e; trying to write to a read-only file).</td></tr>
<tr><td class="topAlign"><code>QSettings::FormatError</code></td><td class="topAlign tblval"><code>2</code></td><td class="topAlign">A format error occurred (e.g&#x2e; loading a malformed INI file).</td></tr>
</table></div>
<p><b>请参考 </b><a href="qsettings.html#status">status</a>()。</p>
<!-- @@@Status -->
<!-- $$$WriteFunc -->
<h3 class="fn" id="WriteFunc-typedef"><a name="WriteFunc-typedef"></a>typedef QSettings::<span class="name">WriteFunc</span></h3>
<p>Typedef for a pointer to a function with the following signature:</p>
<pre class="cpp">

  bool myWriteFunc(<span class="type"><a href="qiodevice.html">QIODevice</a></span> <span class="operator">&amp;</span>device<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span><span class="operator">::</span>SettingsMap <span class="operator">&amp;</span>map);

</pre>
<p><code>WriteFunc</code> is used in <code>registerFormat()</code> as a pointer to a function that writes a set of key/value pairs. <code>WriteFunc</code> is only called once, so you need to output the settings in one go.</p>
<p><b>请参考 </b><a href="qsettings.html#ReadFunc-typedef">ReadFunc</a> 和 <a href="qsettings.html#registerFormat">registerFormat</a>()。</p>
<!-- @@@WriteFunc -->
</div>
<div class="func">
<h2>成员函数文档</h2>
<!-- $$$QSettings[overload1]$$$QSettingsconstQString&constQString&QObject* -->
<h3 class="fn" id="QSettings"><a name="QSettings"></a>QSettings::<span class="name">QSettings</span>(const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>organization</i>, const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>application</i> = QString(), <span class="type"><a href="qobject.html#QObject">QObject</a></span> *<i>parent</i> = nullptr)</h3>
<p>Constructs a <a href="qsettings.html">QSettings</a> object for accessing settings of the application called <i>application</i> from the organization called <i>organization</i>, and with parent <i>parent</i>.</p>
<p>Example:</p>
<pre class="cpp">

  <span class="type">QSettings</span> settings(<span class="string">&quot;Moose Tech&quot;</span><span class="operator">,</span> <span class="string">&quot;Facturo-Pro&quot;</span>);

</pre>
<p>The scope is set to <a href="qsettings.html#Scope-enum">QSettings::UserScope</a>, and the format is set to <a href="qsettings.html#Format-enum">QSettings::NativeFormat</a> (i.e&#x2e; calling <a href="qsettings.html#setDefaultFormat">setDefaultFormat</a>() before calling this constructor has no effect).</p>
<p><b>请参考 </b><a href="qsettings.html#setDefaultFormat">setDefaultFormat</a>() 和 <a href="qsettings.html#fallback-mechanism">Fallback Mechanism</a>。</p>
<!-- @@@QSettings -->
<!-- $$$QSettings$$$QSettingsQSettings::ScopeconstQString&constQString&QObject* -->
<h3 class="fn" id="QSettings-1"><a name="QSettings-1"></a>QSettings::<span class="name">QSettings</span>(<span class="type"><a href="qsettings.html#Scope-enum">QSettings::Scope</a></span> <i>scope</i>, const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>organization</i>, const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>application</i> = QString(), <span class="type"><a href="qobject.html#QObject">QObject</a></span> *<i>parent</i> = nullptr)</h3>
<p>Constructs a <a href="qsettings.html">QSettings</a> object for accessing settings of the application called <i>application</i> from the organization called <i>organization</i>, and with parent <i>parent</i>.</p>
<p>If <i>scope</i> is <a href="qsettings.html#Scope-enum">QSettings::UserScope</a>, the <a href="qsettings.html">QSettings</a> object searches user-specific settings first, before it searches system-wide settings as a fallback. If <i>scope</i> is <a href="qsettings.html#Scope-enum">QSettings::SystemScope</a>, the <a href="qsettings.html">QSettings</a> object ignores user-specific settings and provides access to system-wide settings.</p>
<p>The storage format is set to <a href="qsettings.html#Format-enum">QSettings::NativeFormat</a> (i.e&#x2e; calling <a href="qsettings.html#setDefaultFormat">setDefaultFormat</a>() before calling this constructor has no effect).</p>
<p>If no application name is given, the <a href="qsettings.html">QSettings</a> object will only access the organization-wide <a href="qsettings.html#fallback-mechanism">locations</a>.</p>
<p><b>请参考 </b><a href="qsettings.html#setDefaultFormat">setDefaultFormat</a>()。</p>
<!-- @@@QSettings -->
<!-- $$$QSettings$$$QSettingsQSettings::FormatQSettings::ScopeconstQString&constQString&QObject* -->
<h3 class="fn" id="QSettings-2"><a name="QSettings-2"></a>QSettings::<span class="name">QSettings</span>(<span class="type"><a href="qsettings.html#Format-enum">QSettings::Format</a></span> <i>format</i>, <span class="type"><a href="qsettings.html#Scope-enum">QSettings::Scope</a></span> <i>scope</i>, const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>organization</i>, const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>application</i> = QString(), <span class="type"><a href="qobject.html#QObject">QObject</a></span> *<i>parent</i> = nullptr)</h3>
<p>Constructs a <a href="qsettings.html">QSettings</a> object for accessing settings of the application called <i>application</i> from the organization called <i>organization</i>, and with parent <i>parent</i>.</p>
<p>If <i>scope</i> is <a href="qsettings.html#Scope-enum">QSettings::UserScope</a>, the <a href="qsettings.html">QSettings</a> object searches user-specific settings first, before it searches system-wide settings as a fallback. If <i>scope</i> is <a href="qsettings.html#Scope-enum">QSettings::SystemScope</a>, the <a href="qsettings.html">QSettings</a> object ignores user-specific settings and provides access to system-wide settings.</p>
<p>If <i>format</i> is <a href="qsettings.html#Format-enum">QSettings::NativeFormat</a>, the native API is used for storing settings. If <i>format</i> is <a href="qsettings.html#Format-enum">QSettings::IniFormat</a>, the INI format is used.</p>
<p>If no application name is given, the <a href="qsettings.html">QSettings</a> object will only access the organization-wide <a href="qsettings.html#fallback-mechanism">locations</a>.</p>
<!-- @@@QSettings -->
<!-- $$$QSettings$$$QSettingsconstQString&QSettings::FormatQObject* -->
<h3 class="fn" id="QSettings-3"><a name="QSettings-3"></a>QSettings::<span class="name">QSettings</span>(const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>fileName</i>, <span class="type"><a href="qsettings.html#Format-enum">QSettings::Format</a></span> <i>format</i>, <span class="type"><a href="qobject.html#QObject">QObject</a></span> *<i>parent</i> = nullptr)</h3>
<p>Constructs a <a href="qsettings.html">QSettings</a> object for accessing the settings stored in the file called <i>fileName</i>, with parent <i>parent</i>. If the file doesn't already exist, it is created.</p>
<p>If <i>format</i> is <a href="qsettings.html#Format-enum">QSettings::NativeFormat</a>, the meaning of <i>fileName</i> depends on the platform. On Unix, <i>fileName</i> is the name of an INI file. On <a href="../qtdoc/internationalization.html#macos">macOS</a> and iOS, <i>fileName</i> is the name of a <code>.plist</code> file. On Windows, <i>fileName</i> is a path in the system registry.</p>
<p>If <i>format</i> is <a href="qsettings.html#Format-enum">QSettings::IniFormat</a>, <i>fileName</i> is the name of an INI file.</p>
<p><b>Warning:</b> This function is provided for convenience. It works well for accessing INI or <code>.plist</code> files generated by Qt, but might fail on some syntaxes found in such files originated by other programs. In particular, be aware of the following limitations:</p>
<ul>
<li><a href="qsettings.html">QSettings</a> provides no way of reading INI &quot;path&quot; entries, i.e&#x2e;, entries with unescaped slash characters. (This is because these entries are ambiguous and cannot be resolved automatically.)</li>
<li>In INI files, <a href="qsettings.html">QSettings</a> uses the <code>@</code> character as a metacharacter in some contexts, to encode Qt-specific data types (e.g&#x2e;, <code>@Rect</code>), and might therefore misinterpret it when it occurs in pure INI files.</li>
</ul>
<p><b>请参考 </b><a href="qsettings.html#fileName">fileName</a>()。</p>
<!-- @@@QSettings -->
<!-- $$$QSettings$$$QSettingsQObject* -->
<h3 class="fn" id="QSettings-4"><a name="QSettings-4"></a>QSettings::<span class="name">QSettings</span>(<span class="type"><a href="qobject.html#QObject">QObject</a></span> *<i>parent</i> = nullptr)</h3>
<p>Constructs a <a href="qsettings.html">QSettings</a> object for accessing settings of the application and organization set previously with a call to <a href="qcoreapplication.html#organizationName-prop">QCoreApplication::setOrganizationName</a>(), <a href="qcoreapplication.html#organizationDomain-prop">QCoreApplication::setOrganizationDomain</a>(), and <a href="qcoreapplication.html#applicationName-prop">QCoreApplication::setApplicationName</a>().</p>
<p>The scope is <a href="qsettings.html#Scope-enum">QSettings::UserScope</a> and the format is <a href="qsettings.html#defaultFormat">defaultFormat</a>() (<a href="qsettings.html#Format-enum">QSettings::NativeFormat</a> by default). Use <a href="qsettings.html#setDefaultFormat">setDefaultFormat</a>() before calling this constructor to change the default format used by this constructor.</p>
<p>The code</p>
<pre class="cpp">

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings(<span class="string">&quot;Moose Soft&quot;</span><span class="operator">,</span> <span class="string">&quot;Facturo-Pro&quot;</span>);

</pre>
<p>is equivalent to</p>
<pre class="cpp">

  <span class="type"><a href="qcoreapplication.html">QCoreApplication</a></span><span class="operator">::</span>setOrganizationName(<span class="string">&quot;Moose Soft&quot;</span>);
  <span class="type"><a href="qcoreapplication.html">QCoreApplication</a></span><span class="operator">::</span>setApplicationName(<span class="string">&quot;Facturo-Pro&quot;</span>);
  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings;

</pre>
<p>If <a href="qcoreapplication.html#organizationName-prop">QCoreApplication::setOrganizationName</a>() and <a href="qcoreapplication.html#applicationName-prop">QCoreApplication::setApplicationName</a>() has not been previously called, the <a href="qsettings.html">QSettings</a> object will not be able to read or write any settings, and <a href="qsettings.html#status">status</a>() will return <a href="qsettings.html#Status-enum">AccessError</a>.</p>
<p>On <a href="../qtdoc/internationalization.html#macos">macOS</a> and iOS, if both a name and an Internet domain are specified for the organization, the domain is preferred over the name. On other platforms, the name is preferred over the domain.</p>
<p><b>请参考 </b><a href="qcoreapplication.html#organizationName-prop">QCoreApplication::setOrganizationName</a>(), <a href="qcoreapplication.html#organizationDomain-prop">QCoreApplication::setOrganizationDomain</a>(), <a href="qcoreapplication.html#applicationName-prop">QCoreApplication::setApplicationName</a>(), 和 <a href="qsettings.html#setDefaultFormat">setDefaultFormat</a>()。</p>
<!-- @@@QSettings -->
<!-- $$$~QSettings[overload1]$$$~QSettings -->
<h3 class="fn" id="dtor.QSettings"><a name="dtor.QSettings"></a><code>[virtual] </code>QSettings::<span class="name">~QSettings</span>()</h3>
<p>Destroys the <a href="qsettings.html">QSettings</a> object.</p>
<p>Any unsaved changes will eventually be written to permanent storage.</p>
<p><b>请参考 </b><a href="qsettings.html#sync">sync</a>()。</p>
<!-- @@@~QSettings -->
<!-- $$$allKeys[overload1]$$$allKeys -->
<h3 class="fn" id="allKeys"><a name="allKeys"></a><span class="type"><a href="qstringlist.html">QStringList</a></span> QSettings::<span class="name">allKeys</span>() const</h3>
<p>Returns a list of all keys, including subkeys, that can be read using the <a href="qsettings.html">QSettings</a> object.</p>
<p>Example:</p>
<pre class="cpp">

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings;
  settings<span class="operator">.</span>setValue(<span class="string">&quot;fridge/color&quot;</span><span class="operator">,</span> <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span>(<span class="type">Qt</span><span class="operator">::</span>white));
  settings<span class="operator">.</span>setValue(<span class="string">&quot;fridge/size&quot;</span><span class="operator">,</span> <span class="type"><a href="qsize.html">QSize</a></span>(<span class="number">32</span><span class="operator">,</span> <span class="number">96</span>));
  settings<span class="operator">.</span>setValue(<span class="string">&quot;sofa&quot;</span><span class="operator">,</span> <span class="keyword">true</span>);
  settings<span class="operator">.</span>setValue(<span class="string">&quot;tv&quot;</span><span class="operator">,</span> <span class="keyword">false</span>);

  <span class="type"><a href="qstringlist.html">QStringList</a></span> keys <span class="operator">=</span> settings<span class="operator">.</span>allKeys();
  <span class="comment">// keys: [&quot;fridge/color&quot;, &quot;fridge/size&quot;, &quot;sofa&quot;, &quot;tv&quot;]</span>

</pre>
<p>If a group is set using <a href="qsettings.html#beginGroup">beginGroup</a>(), only the keys in the group are returned, without the group prefix:</p>
<pre class="cpp">

  settings<span class="operator">.</span>beginGroup(<span class="string">&quot;fridge&quot;</span>);
  keys <span class="operator">=</span> settings<span class="operator">.</span>allKeys();
  <span class="comment">// keys: [&quot;color&quot;, &quot;size&quot;]</span>

</pre>
<p><b>请参考 </b><a href="qsettings.html#childGroups">childGroups</a>() 和 <a href="qsettings.html#childKeys">childKeys</a>()。</p>
<!-- @@@allKeys -->
<!-- $$$applicationName[overload1]$$$applicationName -->
<h3 class="fn" id="applicationName"><a name="applicationName"></a><span class="type"><a href="qstring.html">QString</a></span> QSettings::<span class="name">applicationName</span>() const</h3>
<p>Returns the application name used for storing the settings.</p>
<p>该函数于Qt 4.4中引入。</p>
<p><b>请参考 </b><a href="qcoreapplication.html#applicationName-prop">QCoreApplication::applicationName</a>(), <a href="qsettings.html#format">format</a>(), <a href="qsettings.html#scope">scope</a>(), 和 <a href="qsettings.html#organizationName">organizationName</a>()。</p>
<!-- @@@applicationName -->
<!-- $$$beginGroup[overload1]$$$beginGroupconstQString& -->
<h3 class="fn" id="beginGroup"><a name="beginGroup"></a><span class="type">void</span> QSettings::<span class="name">beginGroup</span>(const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>prefix</i>)</h3>
<p>Appends <i>prefix</i> to the current group.</p>
<p>The current group is automatically prepended to all keys specified to <a href="qsettings.html">QSettings</a>. In addition, query functions such as <a href="qsettings.html#childGroups">childGroups</a>(), <a href="qsettings.html#childKeys">childKeys</a>(), and <a href="qsettings.html#allKeys">allKeys</a>() are based on the group. By default, no group is set.</p>
<p>Groups are useful to avoid typing in the same setting paths over and over. For example:</p>
<pre class="cpp">

  settings<span class="operator">.</span>beginGroup(<span class="string">&quot;mainwindow&quot;</span>);
  settings<span class="operator">.</span>setValue(<span class="string">&quot;size&quot;</span><span class="operator">,</span> win<span class="operator">-</span><span class="operator">&gt;</span>size());
  settings<span class="operator">.</span>setValue(<span class="string">&quot;fullScreen&quot;</span><span class="operator">,</span> win<span class="operator">-</span><span class="operator">&gt;</span>isFullScreen());
  settings<span class="operator">.</span>endGroup();

  settings<span class="operator">.</span>beginGroup(<span class="string">&quot;outputpanel&quot;</span>);
  settings<span class="operator">.</span>setValue(<span class="string">&quot;visible&quot;</span><span class="operator">,</span> panel<span class="operator">-</span><span class="operator">&gt;</span>isVisible());
  settings<span class="operator">.</span>endGroup();

</pre>
<p>This will set the value of three settings:</p>
<ul>
<li><code>mainwindow/size</code></li>
<li><code>mainwindow/fullScreen</code></li>
<li><code>outputpanel/visible</code></li>
</ul>
<p>Call <a href="qsettings.html#endGroup">endGroup</a>() to reset the current group to what it was before the corresponding beginGroup() call. Groups can be nested.</p>
<p><b>请参考 </b><a href="qsettings.html#endGroup">endGroup</a>() 和 <a href="qsettings.html#group">group</a>()。</p>
<!-- @@@beginGroup -->
<!-- $$$beginReadArray[overload1]$$$beginReadArrayconstQString& -->
<h3 class="fn" id="beginReadArray"><a name="beginReadArray"></a><span class="type">int</span> QSettings::<span class="name">beginReadArray</span>(const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>prefix</i>)</h3>
<p>Adds <i>prefix</i> to the current group and starts reading from an array. Returns the size of the array.</p>
<p>Example:</p>
<pre class="cpp">

  <span class="keyword">struct</span> Login {
      <span class="type"><a href="qstring.html">QString</a></span> userName;
      <span class="type"><a href="qstring.html">QString</a></span> password;
  };
  <span class="type"><a href="qlist.html">QList</a></span><span class="operator">&lt;</span>Login<span class="operator">&gt;</span> logins;
  <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings;
  <span class="type">int</span> size <span class="operator">=</span> settings<span class="operator">.</span>beginReadArray(<span class="string">&quot;logins&quot;</span>);
  <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> size; <span class="operator">+</span><span class="operator">+</span>i) {
      settings<span class="operator">.</span>setArrayIndex(i);
      Login login;
      login<span class="operator">.</span>userName <span class="operator">=</span> settings<span class="operator">.</span>value(<span class="string">&quot;userName&quot;</span>)<span class="operator">.</span>toString();
      login<span class="operator">.</span>password <span class="operator">=</span> settings<span class="operator">.</span>value(<span class="string">&quot;password&quot;</span>)<span class="operator">.</span>toString();
      logins<span class="operator">.</span>append(login);
  }
  settings<span class="operator">.</span>endArray();

</pre>
<p>Use <a href="qsettings.html#beginWriteArray">beginWriteArray</a>() to write the array in the first place.</p>
<p><b>请参考 </b><a href="qsettings.html#beginWriteArray">beginWriteArray</a>(), <a href="qsettings.html#endArray">endArray</a>(), 和 <a href="qsettings.html#setArrayIndex">setArrayIndex</a>()。</p>
<!-- @@@beginReadArray -->
<!-- $$$beginWriteArray[overload1]$$$beginWriteArrayconstQString&int -->
<h3 class="fn" id="beginWriteArray"><a name="beginWriteArray"></a><span class="type">void</span> QSettings::<span class="name">beginWriteArray</span>(const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>prefix</i>, <span class="type">int</span> <i>size</i> = -1)</h3>
<p>Adds <i>prefix</i> to the current group and starts writing an array of size <i>size</i>. If <i>size</i> is -1 (the default), it is automatically determined based on the indexes of the entries written.</p>
<p>If you have many occurrences of a certain set of keys, you can use arrays to make your life easier. For example, let's suppose that you want to save a variable-length list of user names and passwords. You could then write:</p>
<pre class="cpp">

  <span class="keyword">struct</span> Login {
      <span class="type"><a href="qstring.html">QString</a></span> userName;
      <span class="type"><a href="qstring.html">QString</a></span> password;
  };
  <span class="type"><a href="qlist.html">QList</a></span><span class="operator">&lt;</span>Login<span class="operator">&gt;</span> logins;
  <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings;
  settings<span class="operator">.</span>beginWriteArray(<span class="string">&quot;logins&quot;</span>);
  <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> logins<span class="operator">.</span>size(); <span class="operator">+</span><span class="operator">+</span>i) {
      settings<span class="operator">.</span>setArrayIndex(i);
      settings<span class="operator">.</span>setValue(<span class="string">&quot;userName&quot;</span><span class="operator">,</span> list<span class="operator">.</span>at(i)<span class="operator">.</span>userName);
      settings<span class="operator">.</span>setValue(<span class="string">&quot;password&quot;</span><span class="operator">,</span> list<span class="operator">.</span>at(i)<span class="operator">.</span>password);
  }
  settings<span class="operator">.</span>endArray();

</pre>
<p>The generated keys will have the form</p>
<ul>
<li><code>logins/size</code></li>
<li><code>logins/1/userName</code></li>
<li><code>logins/1/password</code></li>
<li><code>logins/2/userName</code></li>
<li><code>logins/2/password</code></li>
<li><code>logins/3/userName</code></li>
<li><code>logins/3/password</code></li>
<li>...</li>
</ul>
<p>To read back an array, use <a href="qsettings.html#beginReadArray">beginReadArray</a>().</p>
<p><b>请参考 </b><a href="qsettings.html#beginReadArray">beginReadArray</a>(), <a href="qsettings.html#endArray">endArray</a>(), 和 <a href="qsettings.html#setArrayIndex">setArrayIndex</a>()。</p>
<!-- @@@beginWriteArray -->
<!-- $$$childGroups[overload1]$$$childGroups -->
<h3 class="fn" id="childGroups"><a name="childGroups"></a><span class="type"><a href="qstringlist.html">QStringList</a></span> QSettings::<span class="name">childGroups</span>() const</h3>
<p>Returns a list of all key top-level groups that contain keys that can be read using the <a href="qsettings.html">QSettings</a> object.</p>
<p>Example:</p>
<pre class="cpp">

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings;
  settings<span class="operator">.</span>setValue(<span class="string">&quot;fridge/color&quot;</span><span class="operator">,</span> <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span>(<span class="type">Qt</span><span class="operator">::</span>white));
  settings<span class="operator">.</span>setValue(<span class="string">&quot;fridge/size&quot;</span><span class="operator">,</span> <span class="type"><a href="qsize.html">QSize</a></span>(<span class="number">32</span><span class="operator">,</span> <span class="number">96</span>));
  settings<span class="operator">.</span>setValue(<span class="string">&quot;sofa&quot;</span><span class="operator">,</span> <span class="keyword">true</span>);
  settings<span class="operator">.</span>setValue(<span class="string">&quot;tv&quot;</span><span class="operator">,</span> <span class="keyword">false</span>);

  <span class="type"><a href="qstringlist.html">QStringList</a></span> groups <span class="operator">=</span> settings<span class="operator">.</span>childGroups();
  <span class="comment">// groups: [&quot;fridge&quot;]</span>

</pre>
<p>If a group is set using <a href="qsettings.html#beginGroup">beginGroup</a>(), the first-level keys in that group are returned, without the group prefix.</p>
<pre class="cpp">

  settings<span class="operator">.</span>beginGroup(<span class="string">&quot;fridge&quot;</span>);
  groups <span class="operator">=</span> settings<span class="operator">.</span>childGroups();
  <span class="comment">// groups: []</span>

</pre>
<p>You can navigate through the entire setting hierarchy using <a href="qsettings.html#childKeys">childKeys</a>() and childGroups() recursively.</p>
<p><b>请参考 </b><a href="qsettings.html#childKeys">childKeys</a>() 和 <a href="qsettings.html#allKeys">allKeys</a>()。</p>
<!-- @@@childGroups -->
<!-- $$$childKeys[overload1]$$$childKeys -->
<h3 class="fn" id="childKeys"><a name="childKeys"></a><span class="type"><a href="qstringlist.html">QStringList</a></span> QSettings::<span class="name">childKeys</span>() const</h3>
<p>Returns a list of all top-level keys that can be read using the <a href="qsettings.html">QSettings</a> object.</p>
<p>Example:</p>
<pre class="cpp">

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings;
  settings<span class="operator">.</span>setValue(<span class="string">&quot;fridge/color&quot;</span><span class="operator">,</span> <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span>(<span class="type">Qt</span><span class="operator">::</span>white));
  settings<span class="operator">.</span>setValue(<span class="string">&quot;fridge/size&quot;</span><span class="operator">,</span> <span class="type"><a href="qsize.html">QSize</a></span>(<span class="number">32</span><span class="operator">,</span> <span class="number">96</span>));
  settings<span class="operator">.</span>setValue(<span class="string">&quot;sofa&quot;</span><span class="operator">,</span> <span class="keyword">true</span>);
  settings<span class="operator">.</span>setValue(<span class="string">&quot;tv&quot;</span><span class="operator">,</span> <span class="keyword">false</span>);

  <span class="type"><a href="qstringlist.html">QStringList</a></span> keys <span class="operator">=</span> settings<span class="operator">.</span>childKeys();
  <span class="comment">// keys: [&quot;sofa&quot;, &quot;tv&quot;]</span>

</pre>
<p>If a group is set using <a href="qsettings.html#beginGroup">beginGroup</a>(), the top-level keys in that group are returned, without the group prefix:</p>
<pre class="cpp">

  settings<span class="operator">.</span>beginGroup(<span class="string">&quot;fridge&quot;</span>);
  keys <span class="operator">=</span> settings<span class="operator">.</span>childKeys();
  <span class="comment">// keys: [&quot;color&quot;, &quot;size&quot;]</span>

</pre>
<p>You can navigate through the entire setting hierarchy using childKeys() and <a href="qsettings.html#childGroups">childGroups</a>() recursively.</p>
<p><b>请参考 </b><a href="qsettings.html#childGroups">childGroups</a>() 和 <a href="qsettings.html#allKeys">allKeys</a>()。</p>
<!-- @@@childKeys -->
<!-- $$$clear[overload1]$$$clear -->
<h3 class="fn" id="clear"><a name="clear"></a><span class="type">void</span> QSettings::<span class="name">clear</span>()</h3>
<p>Removes all entries in the primary location associated to this <a href="qsettings.html">QSettings</a> object.</p>
<p>Entries in fallback locations are not removed.</p>
<p>If you only want to remove the entries in the current <a href="qsettings.html#group">group</a>(), use remove(&quot;&quot;) instead.</p>
<p><b>请参考 </b><a href="qsettings.html#remove">remove</a>() 和 <a href="qsettings.html#setFallbacksEnabled">setFallbacksEnabled</a>()。</p>
<!-- @@@clear -->
<!-- $$$contains[overload1]$$$containsconstQString& -->
<h3 class="fn" id="contains"><a name="contains"></a><span class="type">bool</span> QSettings::<span class="name">contains</span>(const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>key</i>) const</h3>
<p>Returns <code>true</code> if there exists a setting called <i>key</i>; returns false otherwise.</p>
<p>If a group is set using <a href="qsettings.html#beginGroup">beginGroup</a>(), <i>key</i> is taken to be relative to that group.</p>
<p>Note that the Windows registry and INI files use case-insensitive keys, whereas the CFPreferences API on <a href="../qtdoc/internationalization.html#macos">macOS</a> and iOS uses case-sensitive keys. To avoid portability problems, see the <a href="qsettings.html#section-and-key-syntax">Section and Key Syntax</a> rules.</p>
<p><b>请参考 </b><a href="qsettings.html#value">value</a>() 和 <a href="qsettings.html#setValue">setValue</a>()。</p>
<!-- @@@contains -->
<!-- $$$defaultFormat[overload1]$$$defaultFormat -->
<h3 class="fn" id="defaultFormat"><a name="defaultFormat"></a><code>[static] </code><span class="type"><a href="qsettings.html#Format-enum">QSettings::Format</a></span> QSettings::<span class="name">defaultFormat</span>()</h3>
<p>Returns default file format used for storing settings for the <a href="qsettings.html">QSettings</a>(<a href="qobject.html">QObject</a> *) constructor. If no default format is set, <a href="qsettings.html#Format-enum">QSettings::NativeFormat</a> is used.</p>
<p>该函数于Qt 4.4中引入。</p>
<p><b>请参考 </b><a href="qsettings.html#setDefaultFormat">setDefaultFormat</a>() 和 <a href="qsettings.html#format">format</a>()。</p>
<!-- @@@defaultFormat -->
<!-- $$$endArray[overload1]$$$endArray -->
<h3 class="fn" id="endArray"><a name="endArray"></a><span class="type">void</span> QSettings::<span class="name">endArray</span>()</h3>
<p>Closes the array that was started using <a href="qsettings.html#beginReadArray">beginReadArray</a>() or <a href="qsettings.html#beginWriteArray">beginWriteArray</a>().</p>
<p><b>请参考 </b><a href="qsettings.html#beginReadArray">beginReadArray</a>() 和 <a href="qsettings.html#beginWriteArray">beginWriteArray</a>()。</p>
<!-- @@@endArray -->
<!-- $$$endGroup[overload1]$$$endGroup -->
<h3 class="fn" id="endGroup"><a name="endGroup"></a><span class="type">void</span> QSettings::<span class="name">endGroup</span>()</h3>
<p>Resets the group to what it was before the corresponding <a href="qsettings.html#beginGroup">beginGroup</a>() call.</p>
<p>Example:</p>
<pre class="cpp">

  settings<span class="operator">.</span>beginGroup(<span class="string">&quot;alpha&quot;</span>);
  <span class="comment">// settings.group() == &quot;alpha&quot;</span>

  settings<span class="operator">.</span>beginGroup(<span class="string">&quot;beta&quot;</span>);
  <span class="comment">// settings.group() == &quot;alpha/beta&quot;</span>

  settings<span class="operator">.</span>endGroup();
  <span class="comment">// settings.group() == &quot;alpha&quot;</span>

  settings<span class="operator">.</span>endGroup();
  <span class="comment">// settings.group() == &quot;&quot;</span>

</pre>
<p><b>请参考 </b><a href="qsettings.html#beginGroup">beginGroup</a>() 和 <a href="qsettings.html#group">group</a>()。</p>
<!-- @@@endGroup -->
<!-- $$$event[overload1]$$$eventQEvent* -->
<h3 class="fn" id="event"><a name="event"></a><code>[override virtual protected] </code><span class="type">bool</span> QSettings::<span class="name">event</span>(<span class="type"><a href="qevent.html">QEvent</a></span> *<i>event</i>)</h3>
<p>重新实现自<a href="qobject.html#event">QObject::event</a>()。</p>
<!-- @@@event -->
<!-- $$$fallbacksEnabled[overload1]$$$fallbacksEnabled -->
<h3 class="fn" id="fallbacksEnabled"><a name="fallbacksEnabled"></a><span class="type">bool</span> QSettings::<span class="name">fallbacksEnabled</span>() const</h3>
<p>Returns <code>true</code> if fallbacks are enabled; returns <code>false</code> otherwise.</p>
<p>By default, fallbacks are enabled.</p>
<p><b>请参考 </b><a href="qsettings.html#setFallbacksEnabled">setFallbacksEnabled</a>()。</p>
<!-- @@@fallbacksEnabled -->
<!-- $$$fileName[overload1]$$$fileName -->
<h3 class="fn" id="fileName"><a name="fileName"></a><span class="type"><a href="qstring.html">QString</a></span> QSettings::<span class="name">fileName</span>() const</h3>
<p>Returns the path where settings written using this <a href="qsettings.html">QSettings</a> object are stored.</p>
<p>On Windows, if the format is <a href="qsettings.html#Format-enum">QSettings::NativeFormat</a>, the return value is a system registry path, not a file path.</p>
<p><b>请参考 </b><a href="qsettings.html#isWritable">isWritable</a>() 和 <a href="qsettings.html#format">format</a>()。</p>
<!-- @@@fileName -->
<!-- $$$format[overload1]$$$format -->
<h3 class="fn" id="format"><a name="format"></a><span class="type"><a href="qsettings.html#Format-enum">QSettings::Format</a></span> QSettings::<span class="name">format</span>() const</h3>
<p>Returns the format used for storing the settings.</p>
<p>该函数于Qt 4.4中引入。</p>
<p><b>请参考 </b><a href="qsettings.html#defaultFormat">defaultFormat</a>(), <a href="qsettings.html#fileName">fileName</a>(), <a href="qsettings.html#scope">scope</a>(), <a href="qsettings.html#organizationName">organizationName</a>(), 和 <a href="qsettings.html#applicationName">applicationName</a>()。</p>
<!-- @@@format -->
<!-- $$$group[overload1]$$$group -->
<h3 class="fn" id="group"><a name="group"></a><span class="type"><a href="qstring.html">QString</a></span> QSettings::<span class="name">group</span>() const</h3>
<p>Returns the current group.</p>
<p><b>请参考 </b><a href="qsettings.html#beginGroup">beginGroup</a>() 和 <a href="qsettings.html#endGroup">endGroup</a>()。</p>
<!-- @@@group -->
<!-- $$$iniCodec[overload1]$$$iniCodec -->
<h3 class="fn" id="iniCodec"><a name="iniCodec"></a><span class="type"><a href="qtextcodec.html">QTextCodec</a></span> *QSettings::<span class="name">iniCodec</span>() const</h3>
<p>Returns the codec that is used for accessing INI files. By default, no codec is used, so a null pointer is returned.</p>
<p>该函数于Qt 4.5中引入。</p>
<p><b>请参考 </b><a href="qsettings.html#setIniCodec">setIniCodec</a>()。</p>
<!-- @@@iniCodec -->
<!-- $$$isAtomicSyncRequired[overload1]$$$isAtomicSyncRequired -->
<h3 class="fn" id="isAtomicSyncRequired"><a name="isAtomicSyncRequired"></a><span class="type">bool</span> QSettings::<span class="name">isAtomicSyncRequired</span>() const</h3>
<p>Returns <code>true</code> if <a href="qsettings.html">QSettings</a> is only allowed to perform atomic saving and reloading (synchronization) of the settings. Returns <code>false</code> if it is allowed to save the settings contents directly to the configuration file.</p>
<p>The default is <code>true</code>.</p>
<p>该函数于Qt 5.10中引入。</p>
<p><b>请参考 </b><a href="qsettings.html#setAtomicSyncRequired">setAtomicSyncRequired</a>() 和 <a href="qsavefile.html">QSaveFile</a>。</p>
<!-- @@@isAtomicSyncRequired -->
<!-- $$$isWritable[overload1]$$$isWritable -->
<h3 class="fn" id="isWritable"><a name="isWritable"></a><span class="type">bool</span> QSettings::<span class="name">isWritable</span>() const</h3>
<p>Returns <code>true</code> if settings can be written using this <a href="qsettings.html">QSettings</a> object; returns <code>false</code> otherwise.</p>
<p>One reason why isWritable() might return false is if <a href="qsettings.html">QSettings</a> operates on a read-only file.</p>
<p><b>Warning:</b> This function is not perfectly reliable, because the file permissions can change at any time.</p>
<p><b>请参考 </b><a href="qsettings.html#fileName">fileName</a>(), <a href="qsettings.html#status">status</a>(), 和 <a href="qsettings.html#sync">sync</a>()。</p>
<!-- @@@isWritable -->
<!-- $$$organizationName[overload1]$$$organizationName -->
<h3 class="fn" id="organizationName"><a name="organizationName"></a><span class="type"><a href="qstring.html">QString</a></span> QSettings::<span class="name">organizationName</span>() const</h3>
<p>Returns the organization name used for storing the settings.</p>
<p>该函数于Qt 4.4中引入。</p>
<p><b>请参考 </b><a href="qcoreapplication.html#organizationName-prop">QCoreApplication::organizationName</a>(), <a href="qsettings.html#format">format</a>(), <a href="qsettings.html#scope">scope</a>(), 和 <a href="qsettings.html#applicationName">applicationName</a>()。</p>
<!-- @@@organizationName -->
<!-- $$$registerFormat[overload1]$$$registerFormatconstQString&QSettings::ReadFuncQSettings::WriteFuncQt::CaseSensitivity -->
<h3 class="fn" id="registerFormat"><a name="registerFormat"></a><code>[static] </code><span class="type"><a href="qsettings.html#Format-enum">QSettings::Format</a></span> QSettings::<span class="name">registerFormat</span>(const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>extension</i>, <span class="type"><a href="qsettings.html#ReadFunc-typedef">QSettings::ReadFunc</a></span> <i>readFunc</i>, <span class="type"><a href="qsettings.html#WriteFunc-typedef">QSettings::WriteFunc</a></span> <i>writeFunc</i>, <span class="type"><a href="qt.html#CaseSensitivity-enum">Qt::CaseSensitivity</a></span> <i>caseSensitivity</i> = Qt::CaseSensitive)</h3>
<p>Registers a custom storage format. On success, returns a special Format value that can then be passed to the <a href="qsettings.html">QSettings</a> constructor. On failure, returns <a href="qsettings.html#Format-enum">InvalidFormat</a>.</p>
<p>The <i>extension</i> is the file extension associated to the format (without the '.').</p>
<p>The <i>readFunc</i> and <i>writeFunc</i> parameters are pointers to functions that read and write a set of key/value pairs. The <a href="qiodevice.html">QIODevice</a> parameter to the read and write functions is always opened in binary mode (i.e&#x2e;, without the <a href="qiodevice.html#OpenModeFlag-enum">QIODevice::Text</a> flag).</p>
<p>The <i>caseSensitivity</i> parameter specifies whether keys are case sensitive or not. This makes a difference when looking up values using <a href="qsettings.html">QSettings</a>. The default is case sensitive.</p>
<p>By default, if you use one of the constructors that work in terms of an organization name and an application name, the file system locations used are the same as for <a href="qsettings.html#Format-enum">IniFormat</a>. Use <a href="qsettings.html#setPath">setPath</a>() to specify other locations.</p>
<p>Example:</p>
<pre class="cpp">

  bool readXmlFile(<span class="type"><a href="qiodevice.html">QIODevice</a></span> <span class="operator">&amp;</span>device<span class="operator">,</span> <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span><span class="operator">::</span>SettingsMap <span class="operator">&amp;</span>map);
  bool writeXmlFile(<span class="type"><a href="qiodevice.html">QIODevice</a></span> <span class="operator">&amp;</span>device<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span><span class="operator">::</span>SettingsMap <span class="operator">&amp;</span>map);

  <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
  {
      <span class="keyword">const</span> <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span><span class="operator">::</span>Format XmlFormat <span class="operator">=</span>
              <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span><span class="operator">::</span>registerFormat(<span class="string">&quot;xml&quot;</span><span class="operator">,</span> readXmlFile<span class="operator">,</span> writeXmlFile);

      <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings(XmlFormat<span class="operator">,</span> <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span><span class="operator">::</span>UserScope<span class="operator">,</span> <span class="string">&quot;MySoft&quot;</span><span class="operator">,</span>
                         <span class="string">&quot;Star Runner&quot;</span>);

      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
  }

</pre>
<p><b>Note:</b> This function is <a href="../qtdoc/threads-reentrancy.html">thread-safe</a>.</p>
<p>该函数于Qt 4.1中引入。</p>
<p><b>请参考 </b><a href="qsettings.html#setPath">setPath</a>()。</p>
<!-- @@@registerFormat -->
<!-- $$$remove[overload1]$$$removeconstQString& -->
<h3 class="fn" id="remove"><a name="remove"></a><span class="type">void</span> QSettings::<span class="name">remove</span>(const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>key</i>)</h3>
<p>Removes the setting <i>key</i> and any sub-settings of <i>key</i>.</p>
<p>Example:</p>
<pre class="cpp">

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings;
  settings<span class="operator">.</span>setValue(<span class="string">&quot;ape&quot;</span>);
  settings<span class="operator">.</span>setValue(<span class="string">&quot;monkey&quot;</span><span class="operator">,</span> <span class="number">1</span>);
  settings<span class="operator">.</span>setValue(<span class="string">&quot;monkey/sea&quot;</span><span class="operator">,</span> <span class="number">2</span>);
  settings<span class="operator">.</span>setValue(<span class="string">&quot;monkey/doe&quot;</span><span class="operator">,</span> <span class="number">4</span>);

  settings<span class="operator">.</span>remove(<span class="string">&quot;monkey&quot;</span>);
  <span class="type"><a href="qstringlist.html">QStringList</a></span> keys <span class="operator">=</span> settings<span class="operator">.</span>allKeys();
  <span class="comment">// keys: [&quot;ape&quot;]</span>

</pre>
<p>Be aware that if one of the fallback locations contains a setting with the same key, that setting will be visible after calling remove().</p>
<p>If <i>key</i> is an empty string, all keys in the current <a href="qsettings.html#group">group</a>() are removed. For example:</p>
<pre class="cpp">

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings;
  settings<span class="operator">.</span>setValue(<span class="string">&quot;ape&quot;</span>);
  settings<span class="operator">.</span>setValue(<span class="string">&quot;monkey&quot;</span><span class="operator">,</span> <span class="number">1</span>);
  settings<span class="operator">.</span>setValue(<span class="string">&quot;monkey/sea&quot;</span><span class="operator">,</span> <span class="number">2</span>);
  settings<span class="operator">.</span>setValue(<span class="string">&quot;monkey/doe&quot;</span><span class="operator">,</span> <span class="number">4</span>);

  settings<span class="operator">.</span>beginGroup(<span class="string">&quot;monkey&quot;</span>);
  settings<span class="operator">.</span>remove(<span class="string">&quot;&quot;</span>);
  settings<span class="operator">.</span>endGroup();

  <span class="type"><a href="qstringlist.html">QStringList</a></span> keys <span class="operator">=</span> settings<span class="operator">.</span>allKeys();
  <span class="comment">// keys: [&quot;ape&quot;]</span>

</pre>
<p>Note that the Windows registry and INI files use case-insensitive keys, whereas the CFPreferences API on <a href="../qtdoc/internationalization.html#macos">macOS</a> and iOS uses case-sensitive keys. To avoid portability problems, see the <a href="qsettings.html#section-and-key-syntax">Section and Key Syntax</a> rules.</p>
<p><b>请参考 </b><a href="qsettings.html#setValue">setValue</a>(), <a href="qsettings.html#value">value</a>(), 和 <a href="qsettings.html#contains">contains</a>()。</p>
<!-- @@@remove -->
<!-- $$$scope[overload1]$$$scope -->
<h3 class="fn" id="scope"><a name="scope"></a><span class="type"><a href="qsettings.html#Scope-enum">QSettings::Scope</a></span> QSettings::<span class="name">scope</span>() const</h3>
<p>Returns the scope used for storing the settings.</p>
<p>该函数于Qt 4.4中引入。</p>
<p><b>请参考 </b><a href="qsettings.html#format">format</a>(), <a href="qsettings.html#organizationName">organizationName</a>(), 和 <a href="qsettings.html#applicationName">applicationName</a>()。</p>
<!-- @@@scope -->
<!-- $$$setArrayIndex[overload1]$$$setArrayIndexint -->
<h3 class="fn" id="setArrayIndex"><a name="setArrayIndex"></a><span class="type">void</span> QSettings::<span class="name">setArrayIndex</span>(<span class="type">int</span> <i>i</i>)</h3>
<p>Sets the current array index to <i>i</i>. Calls to functions such as <a href="qsettings.html#setValue">setValue</a>(), <a href="qsettings.html#value">value</a>(), <a href="qsettings.html#remove">remove</a>(), and <a href="qsettings.html#contains">contains</a>() will operate on the array entry at that index.</p>
<p>You must call <a href="qsettings.html#beginReadArray">beginReadArray</a>() or <a href="qsettings.html#beginWriteArray">beginWriteArray</a>() before you can call this function.</p>
<!-- @@@setArrayIndex -->
<!-- $$$setAtomicSyncRequired[overload1]$$$setAtomicSyncRequiredbool -->
<h3 class="fn" id="setAtomicSyncRequired"><a name="setAtomicSyncRequired"></a><span class="type">void</span> QSettings::<span class="name">setAtomicSyncRequired</span>(<span class="type">bool</span> <i>enable</i>)</h3>
<p>Configures whether <a href="qsettings.html">QSettings</a> is required to perform atomic saving and reloading (synchronization) of the settings. If the <i>enable</i> argument is <code>true</code> (the default), <a href="qsettings.html#sync">sync</a>() will only perform synchronization operations that are atomic. If this is not possible, <a href="qsettings.html#sync">sync</a>() will fail and <a href="qsettings.html#status">status</a>() will be an error condition.</p>
<p>Setting this property to <code>false</code> will allow <a href="qsettings.html">QSettings</a> to write directly to the configuration file and ignore any errors trying to lock it against other processes trying to write at the same time. Because of the potential for corruption, this option should be used with care, but is required in certain conditions, like a <a href="qsettings.html#Format-enum">QSettings::IniFormat</a> configuration file that exists in an otherwise non-writeable directory or NTFS Alternate Data Streams.</p>
<p>See <a href="qsavefile.html">QSaveFile</a> for more information on the feature.</p>
<p>该函数于Qt 5.10中引入。</p>
<p><b>请参考 </b><a href="qsettings.html#isAtomicSyncRequired">isAtomicSyncRequired</a>() 和 <a href="qsavefile.html">QSaveFile</a>。</p>
<!-- @@@setAtomicSyncRequired -->
<!-- $$$setDefaultFormat[overload1]$$$setDefaultFormatQSettings::Format -->
<h3 class="fn" id="setDefaultFormat"><a name="setDefaultFormat"></a><code>[static] </code><span class="type">void</span> QSettings::<span class="name">setDefaultFormat</span>(<span class="type"><a href="qsettings.html#Format-enum">QSettings::Format</a></span> <i>format</i>)</h3>
<p>Sets the default file format to the given <i>format</i>, which is used for storing settings for the <a href="qsettings.html">QSettings</a>(<a href="qobject.html">QObject</a> *) constructor.</p>
<p>If no default format is set, <a href="qsettings.html#Format-enum">QSettings::NativeFormat</a> is used. See the documentation for the <a href="qsettings.html">QSettings</a> constructor you are using to see if that constructor will ignore this function.</p>
<p>该函数于Qt 4.4中引入。</p>
<p><b>请参考 </b><a href="qsettings.html#defaultFormat">defaultFormat</a>() 和 <a href="qsettings.html#format">format</a>()。</p>
<!-- @@@setDefaultFormat -->
<!-- $$$setFallbacksEnabled[overload1]$$$setFallbacksEnabledbool -->
<h3 class="fn" id="setFallbacksEnabled"><a name="setFallbacksEnabled"></a><span class="type">void</span> QSettings::<span class="name">setFallbacksEnabled</span>(<span class="type">bool</span> <i>b</i>)</h3>
<p>Sets whether fallbacks are enabled to <i>b</i>.</p>
<p>By default, fallbacks are enabled.</p>
<p><b>请参考 </b><a href="qsettings.html#fallbacksEnabled">fallbacksEnabled</a>()。</p>
<!-- @@@setFallbacksEnabled -->
<!-- $$$setIniCodec[overload1]$$$setIniCodecQTextCodec* -->
<h3 class="fn" id="setIniCodec"><a name="setIniCodec"></a><span class="type">void</span> QSettings::<span class="name">setIniCodec</span>(<span class="type"><a href="qtextcodec.html">QTextCodec</a></span> *<i>codec</i>)</h3>
<p>Sets the codec for accessing INI files (including <code>.conf</code> files on Unix) to <i>codec</i>. The codec is used for decoding any data that is read from the INI file, and for encoding any data that is written to the file. By default, no codec is used, and non-ASCII characters are encoded using standard INI escape sequences.</p>
<p><b>Warning:</b> The codec must be set immediately after creating the <a href="qsettings.html">QSettings</a> object, before accessing any data.</p>
<p>该函数于Qt 4.5中引入。</p>
<p><b>请参考 </b><a href="qsettings.html#iniCodec">iniCodec</a>()。</p>
<!-- @@@setIniCodec -->
<!-- $$$setIniCodec$$$setIniCodecconstchar* -->
<h3 class="fn" id="setIniCodec-1"><a name="setIniCodec-1"></a><span class="type">void</span> QSettings::<span class="name">setIniCodec</span>(const <span class="type">char</span> *<i>codecName</i>)</h3>
<p>这是一个重载函数。</p>
<p>Sets the codec for accessing INI files (including <code>.conf</code> files on Unix) to the <a href="qtextcodec.html">QTextCodec</a> for the encoding specified by <i>codecName</i>. Common values for <code>codecName</code> include &quot;ISO 8859-1&quot;, &quot;UTF-8&quot;, and &quot;UTF-16&quot;. If the encoding isn't recognized, nothing happens.</p>
<p>该函数于Qt 4.5中引入。</p>
<p><b>请参考 </b><a href="qtextcodec.html#codecForName">QTextCodec::codecForName</a>()。</p>
<!-- @@@setIniCodec -->
<!-- $$$setPath[overload1]$$$setPathQSettings::FormatQSettings::ScopeconstQString& -->
<h3 class="fn" id="setPath"><a name="setPath"></a><code>[static] </code><span class="type">void</span> QSettings::<span class="name">setPath</span>(<span class="type"><a href="qsettings.html#Format-enum">QSettings::Format</a></span> <i>format</i>, <span class="type"><a href="qsettings.html#Scope-enum">QSettings::Scope</a></span> <i>scope</i>, const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>path</i>)</h3>
<p>Sets the path used for storing settings for the given <i>format</i> and <i>scope</i>, to <i>path</i>. The <i>format</i> can be a custom format.</p>
<p>The table below summarizes the default values:</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><th >Platform</th><th >Format</th><th >Scope</th><th >Path</th></tr></thead>
<tr valign="top" class="odd"><td  rowspan="2">Windows</td><td  rowspan="2"><a href="qsettings.html#Format-enum">IniFormat</a></td><td ><a href="qsettings.html#Scope-enum">UserScope</a></td><td ><code>FOLDERID_RoamingAppData</code></td></tr>
<tr valign="top" class="even"><td ><a href="qsettings.html#Scope-enum">SystemScope</a></td><td ><code>FOLDERID_ProgramData</code></td></tr>
<tr valign="top" class="odd"><td  rowspan="2">Unix</td><td  rowspan="2"><a href="qsettings.html#Format-enum">NativeFormat</a>, <a href="qsettings.html#Format-enum">IniFormat</a></td><td ><a href="qsettings.html#Scope-enum">UserScope</a></td><td ><code>$HOME/.config</code></td></tr>
<tr valign="top" class="even"><td ><a href="qsettings.html#Scope-enum">SystemScope</a></td><td ><code>/etc/xdg</code></td></tr>
<tr valign="top" class="odd"><td  rowspan="2">Qt for Embedded Linux</td><td  rowspan="2"><a href="qsettings.html#Format-enum">NativeFormat</a>, <a href="qsettings.html#Format-enum">IniFormat</a></td><td ><a href="qsettings.html#Scope-enum">UserScope</a></td><td ><code>$HOME/Settings</code></td></tr>
<tr valign="top" class="even"><td ><a href="qsettings.html#Scope-enum">SystemScope</a></td><td ><code>/etc/xdg</code></td></tr>
<tr valign="top" class="odd"><td  rowspan="2"><a href="../qtdoc/internationalization.html#macos">macOS</a> and iOS</td><td  rowspan="2"><a href="qsettings.html#Format-enum">IniFormat</a></td><td ><a href="qsettings.html#Scope-enum">UserScope</a></td><td ><code>$HOME/.config</code></td></tr>
<tr valign="top" class="even"><td ><a href="qsettings.html#Scope-enum">SystemScope</a></td><td ><code>/etc/xdg</code></td></tr>
</table></div>
<p>The default <a href="qsettings.html#Scope-enum">UserScope</a> paths on Unix, <a href="../qtdoc/internationalization.html#macos">macOS</a>, and iOS (<code>$HOME/.config</code> or $HOME/Settings) can be overridden by the user by setting the <code>XDG_CONFIG_HOME</code> environment variable. The default <a href="qsettings.html#Scope-enum">SystemScope</a> paths on Unix, <a href="../qtdoc/internationalization.html#macos">macOS</a>, and iOS (<code>/etc/xdg</code>) can be overridden when building the Qt library using the <code>configure</code> script's <code>-sysconfdir</code> flag (see <a href="qlibraryinfo.html">QLibraryInfo</a> for details).</p>
<p>Setting the <a href="qsettings.html#Format-enum">NativeFormat</a> paths on Windows, <a href="../qtdoc/internationalization.html#macos">macOS</a>, and iOS has no effect.</p>
<p><b>Warning:</b> This function doesn't affect existing <a href="qsettings.html">QSettings</a> objects.</p>
<p>该函数于Qt 4.1中引入。</p>
<p><b>请参考 </b><a href="qsettings.html#registerFormat">registerFormat</a>()。</p>
<!-- @@@setPath -->
<!-- $$$setValue[overload1]$$$setValueconstQString&constQVariant& -->
<h3 class="fn" id="setValue"><a name="setValue"></a><span class="type">void</span> QSettings::<span class="name">setValue</span>(const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>key</i>, const <span class="type"><a href="qvariant.html">QVariant</a></span> &amp;<i>value</i>)</h3>
<p>Sets the value of setting <i>key</i> to <i>value</i>. If the <i>key</i> already exists, the previous value is overwritten.</p>
<p>Note that the Windows registry and INI files use case-insensitive keys, whereas the CFPreferences API on <a href="../qtdoc/internationalization.html#macos">macOS</a> and iOS uses case-sensitive keys. To avoid portability problems, see the <a href="qsettings.html#section-and-key-syntax">Section and Key Syntax</a> rules.</p>
<p>Example:</p>
<pre class="cpp">

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings;
  settings<span class="operator">.</span>setValue(<span class="string">&quot;interval&quot;</span><span class="operator">,</span> <span class="number">30</span>);
  settings<span class="operator">.</span>value(<span class="string">&quot;interval&quot;</span>)<span class="operator">.</span>toInt();     <span class="comment">// returns 30</span>

  settings<span class="operator">.</span>setValue(<span class="string">&quot;interval&quot;</span><span class="operator">,</span> <span class="number">6.55</span>);
  settings<span class="operator">.</span>value(<span class="string">&quot;interval&quot;</span>)<span class="operator">.</span>toDouble();  <span class="comment">// returns 6.55</span>

</pre>
<p><b>请参考 </b><a href="qsettings.html#value">value</a>(), <a href="qsettings.html#remove">remove</a>(), 和 <a href="qsettings.html#contains">contains</a>()。</p>
<!-- @@@setValue -->
<!-- $$$status[overload1]$$$status -->
<h3 class="fn" id="status"><a name="status"></a><span class="type"><a href="qsettings.html#Status-enum">QSettings::Status</a></span> QSettings::<span class="name">status</span>() const</h3>
<p>Returns a status code indicating the first error that was met by <a href="qsettings.html">QSettings</a>, or <a href="qsettings.html#Status-enum">QSettings::NoError</a> if no error occurred.</p>
<p>Be aware that <a href="qsettings.html">QSettings</a> delays performing some operations. For this reason, you might want to call <a href="qsettings.html#sync">sync</a>() to ensure that the data stored in <a href="qsettings.html">QSettings</a> is written to disk before calling status().</p>
<p><b>请参考 </b><a href="qsettings.html#sync">sync</a>()。</p>
<!-- @@@status -->
<!-- $$$sync[overload1]$$$sync -->
<h3 class="fn" id="sync"><a name="sync"></a><span class="type">void</span> QSettings::<span class="name">sync</span>()</h3>
<p>Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in the meantime by another application.</p>
<p>This function is called automatically from <a href="qsettings.html">QSettings</a>'s destructor and by the event loop at regular intervals, so you normally don't need to call it yourself.</p>
<p><b>请参考 </b><a href="qsettings.html#status">status</a>()。</p>
<!-- @@@sync -->
<!-- $$$value[overload1]$$$valueconstQString&constQVariant& -->
<h3 class="fn" id="value"><a name="value"></a><span class="type"><a href="qvariant.html">QVariant</a></span> QSettings::<span class="name">value</span>(const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>key</i>, const <span class="type"><a href="qvariant.html">QVariant</a></span> &amp;<i>defaultValue</i> = QVariant()) const</h3>
<p>Returns the value for setting <i>key</i>. If the setting doesn't exist, returns <i>defaultValue</i>.</p>
<p>If no default value is specified, a default <a href="qvariant.html">QVariant</a> is returned.</p>
<p>Note that the Windows registry and INI files use case-insensitive keys, whereas the CFPreferences API on <a href="../qtdoc/internationalization.html#macos">macOS</a> and iOS uses case-sensitive keys. To avoid portability problems, see the <a href="qsettings.html#section-and-key-syntax">Section and Key Syntax</a> rules.</p>
<p>Example:</p>
<pre class="cpp">

  <span class="type"><a href="qsettings.html#QSettings">QSettings</a></span> settings;
  settings<span class="operator">.</span>setValue(<span class="string">&quot;animal/snake&quot;</span><span class="operator">,</span> <span class="number">58</span>);
  settings<span class="operator">.</span>value(<span class="string">&quot;animal/snake&quot;</span><span class="operator">,</span> <span class="number">1024</span>)<span class="operator">.</span>toInt();   <span class="comment">// returns 58</span>
  settings<span class="operator">.</span>value(<span class="string">&quot;animal/zebra&quot;</span><span class="operator">,</span> <span class="number">1024</span>)<span class="operator">.</span>toInt();   <span class="comment">// returns 1024</span>
  settings<span class="operator">.</span>value(<span class="string">&quot;animal/zebra&quot;</span>)<span class="operator">.</span>toInt();         <span class="comment">// returns 0</span>

</pre>
<p><b>请参考 </b><a href="qsettings.html#setValue">setValue</a>(), <a href="qsettings.html#contains">contains</a>(), 和 <a href="qsettings.html#remove">remove</a>()。</p>
<!-- @@@value -->
</div>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
